gpt4 book ai didi

postgresql - Cocoon、Bootstrap_Form_For 和 PostgreSQL 更新除 bool 复选框之外的所有字段

转载 作者:行者123 更新时间:2023-11-29 13:54:23 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

问题发生在为我的复选框提供 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 %>

最终解决了我的问题。

关于postgresql - Cocoon、Bootstrap_Form_For 和 PostgreSQL 更新除 bool 复选框之外的所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com