2

何らかの理由で私の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"}

製品の1つに空の値を渡すためです。値のない空の入力フィールドを手動で削除しても、問題はありません。

4

1 に答える 1

3

これはSimpleFormのものではなく、Railsのものです。Rails 4にはこのための構成オプションがありますが、手動で削除する必要があります。

あなたはここでこれについて少し読むことができます

于 2013-02-19T08:17:09.633 に答える