Cocoon,Bootstrap_Form_For和PostgreSQL更新所有字段但布尔复选框

时间:2016-01-28 15:19:52

标签: postgresql ruby-on-rails-4 nested-forms cocoon-gem

我在rails 4应用程序中使用带有Bootstrap_Form_for的Cocoon Gem。一切正常,我创建和更新所有字段都没有问题,除非我尝试更改已设置的布尔字段。

您将在下面找到我的代码,非常感谢所有帮助。

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 -->

嵌套的Cocoon表格:

<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>

1 个答案:

答案 0 :(得分:0)

问题出现在为我的复选框提供了bootstrap开关的“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  %>
   

最终解决了我的问题。