2016-01-28 23 views
0

我在我的rails 4應用程序中使用Cocoon Gem和Bootstrap_Form_for。一切正常,除了當我嘗試更改已經設置的布爾字段時,創建和更新所有字段都沒有問題。Cocoon,Bootstrap_Form_For和PostgreSQL更新所有字段,但布爾值複選框

下面你會發現我的代碼,任何和所有的幫助,非常感謝。

products_controller:

def update 
    respond_to do |format| 
     if @product.update(product_params) 
     format.html { redirect_to @product, notice: 'Product was successfully updated.' } 
     format.json { render :show, status: :ok, location: @product } 
     else 
     format.html { render :edit } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

categories_controller:

def create 
@category = Category.new(category_params) 

respond_to do |format| 
    if @category.save 
    format.html { redirect_to hq_company_settings_path(:anchor => "productsTab"), notice: 'Category was successfully created.' } 
    format.js { redirect_to hq_company_settings_path(:anchor => "productsTab") } 
    else 
    format.html { redirect_to hq_company_settings_path(:anchor => "productsTab") } 
    format.json { render json: @category.errors, status: :unprocessable_entity } 
    end 
end 

分類模型:

belongs_to :company 
has_many :products 

accepts_nested_attributes_for :products, reject_if: :all_blank, allow_destroy: true 

形式:

<!-- Add Products Form --> 
        <div> 
         <%= bootstrap_form_for(@category) do |f| %> 
         <% if @category.errors.any? %> 
         <div id="error_explanation"> 
          <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2> 
          <ul> 
           <% @category.errors.full_messages.each do |message| %> 
           <li><%= message %></li> 
           <% end %> 
          </ul> 
         </div> 
         <% end %> 
         <fieldset> 
          <div class="input-group margin-bottom-20"> 
           <span class="input-group-addon"><i class="fa fa-book"></i></span> 
           <%= f.text_field :name, hide_label: true %> 
          </div> 
         </fieldset> 
         <div> 
          <div> 
           <fieldset id="products"> 
            <%= f.fields_for :products do |product| %> 
            <%= render 'product_fields', :f => product %> 
            <% end %> 
           </fieldset> 
          </div> 
          <div class="links margin-top-40"> 
           <%= link_to_add_association ' Add Product', f, :products, class:"btn-u btn-u-blue btn-block" %> 
          </div> 
          <hr> 
         </div> 
         <div class="row"> 

          <div class="col-md-12"> 
           <span class="pull-right"> 
            <%= f.submit "Save", class:"btn-u btn-block" %> 
           </span> 
          </div> 
         </div> 
         <% end %> 
        </div> <!-- End Form Wrapper --> 

嵌套繭形式:

<div class="nested-fields"> 
<div class="col-lg-9"> 
     <div class="input-group margin-bottom-20"> 
     <span class="input-group-addon"><i class="fa fa-plus"></i></span> 
     <%= f.text_field :name, hide_label: true, placeholder: 'Product Name' %> 
    </div> 
</div> 
<div class="col-lg-3"> 
    <div class="input-group margin-bottom-20"> 
    <span class="input-group-addon"><i class="fa fa-money"></i></span> 
    <%= f.text_field :price, hide_label: true, placeholder: 'Price' %> 
</div> 
</div> 
<div class="col-lg-4"> 
    <div class="input-group margin-bottom-20"> 
    <span class="input-group-addon"><i class="fa fa-tags"></i></span> 
    <%= f.text_field :sku, hide_label: true, placeholder: 'SKU/ID #' %> 
</div> 
</div> 
<div class="col-md-4"> 
    <div class="input-group margin-bottom-20"> 
    <%= f.hidden_field :company_id, hide_label: true, value: current_user.company_id %> 
    <%= f.form_group :tracking do %> 
     <%= f.check_box :tracking, label: " Inventory Tracking", class: 'radio1 js-switch', name: 'my-checkbox', inline: true %> 
    <% end %> 
    </div> 
</div> 
<div class="col-md-4"> 
    <div class="input-group txt1 margin-top-1"> 
     <span class="input-group-addon"><i class="fa fa-cart-plus"></i></span> 
     <%= f.text_field :inventory, hide_label: true, class: "txt1", placeholder: 'Quantity' %> 
    </div> 
</div> 
<div class="col-md-6"> 
    <div class="input-group txt2 margin-bottom-20"> 
     <span class="input-group-addon"><i class="fa fa-cart-plus"></i></span> 
     <%= f.text_field :minimum, hide_label: true, class: "txt2", placeholder: 'Min. Quantity' %> 
    </div> 
</div> 
<p class="links col-md-6"> 
    <%= link_to_remove_association " Remove Product", f, class:"btn-u btn-u-red btn-block" %> 
</p> 
</div> 

回答

0

問題從給我的複選框 「NAME」 屬性自舉開關發生。

刪除

name: 'my-checkbox' 

<%= f.form_group :tracking do %> 
    <%= f.check_box :tracking, label: " Inventory Tracking", class: 'radio1 js-switch', name: 'my-checkbox', inline: true %> 

結束了固定我的問題。