2013-02-19 35 views
1

出於某種原因,我simple_form代碼形式簡單形式的協會創建空的輸入字段

<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協會形式下插入

<input name="order[product_ids][]" type="hidden" value=""> 

<%= 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"} 

,因爲它通行證的產品之一爲空值。當我手動刪除沒有值的空輸入字段時,它沒有問題。

回答

3

這不是SimpleForm的東西,但Rails的。在Rails 4中會有一個配置選項,但現在你必須手動刪除它。

你可以讀一下這個here

+0

謝謝,永遠不會找到這個。雖然,如果以前的功能我仍然感到困惑。是一個功能或錯誤。 – maschwenk 2013-02-19 15:37:45

+1

如何在simple_form中使用':include_hidden'功能來避免這個'空ID'問題? – 2013-08-17 04:24:45

+0

在Rails 3.2.14中,人們如何處理這個問題?目前還無法遷移到Rails4。 – codeObserver 2013-08-26 04:24:13