2011-08-10 23 views
0

這裏是我的代碼段工程完全按照預期:Rails的collection_select沒有工作,但相當於選擇與options_from_collection_for_select確實

<%= f.select(:other_model_id, 
      options_from_collection_for_select(
       OtherModel.all, 
       :id, 
       :full_name, 
       { :selected => @this_model.other_model_id})) %> 

但由於某些原因,這不起作用:

<%= f.collection_select :this_model, :other_model_id, 
         OtherModel.all, :id, :full_name %> 

有我得到的錯誤是:

未定義的方法`merge'爲:full_name:符號

有什麼建議嗎?事實:full_name與工作代碼正常工作,導致我相信我搞砸了簡化的collection_select代碼中的語法,並且問題不在其他地方。

+0

嘗試爲參數添加'()'。否則嘗試添加最後一個參數爲「{}」。 – Zabba

回答

3

我認爲你在混合使用兩種不同的collection_select方法。您使用FormOptionsHelper#collection_select參數調用FormBuilder#collection_select。也許你想這樣的:

<%= f.collection_select :other_model_id, OtherModel.all, :id, :full_name %> 

或許這樣的:

<%= collection_select :this_model, :other_model_id, OtherModel.all, :id, :full_name %> 

你最終試圖把:full_nameoptions說法,但認爲應該是一個Hash,這就是關於「沒有merge投訴方法「來自。

+0

是的,放棄:this_model作爲第一個參數做了伎倆。 – LikeMaBell

相關問題