出于某种原因,我的 simple_form 代码正在插入
<input name="order[product_ids][]" type="hidden" value="">
在表格下
<label class="checkbox">
<input class="check_boxes optional" id="order_product_ids_11"
name="order[product_ids][]" type="checkbox" value="11">Product11
</label>
当我使用我的 has_and_belongs_to_many 关联表格时
<%= f.association :products, as: :check_boxes, include_blank: false %>
带模型订单
class Order < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :products
和产品
class Product < ActiveRecord::Base
has_and_belongs_to_many :orders
导致错误:
ActiveRecord::RecordNotFound in OrdersController#create
Couldn't find all Products with IDs (3, 5, ) (found 2 results, but was looking for 3)
{"utf8"=>"✓",
"authenticity_token"=>"F0c4J81QXRNMFDXrN55XrRafwj86lzUl3kXe/xXxKxc=",
"order"=>{"order_type"=>"init_purchase",
"shipping_status"=>"unshipped",
"product_ids"=>["3",
"5",
""]},
"commit"=>"Create Order"}
因为它为其中一种产品传递了一个空值。当我手动删除没有值的空输入字段时,它没有问题。