0

意図したとおりに機能するコードスニペットは次のとおりです。

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

私が得るエラーは次のとおりです。

:full_name:Symbolの未定義のメソッド `merge'

助言がありますか?:full_nameが機能するコードで適切に機能するという事実は、私が単純化されたcollection_selectコードの構文を台無しにしたこと、そして問題が他の場所にないことを私に信じさせます。

4

1 に答える 1

3

collection_selectあなたは2つの異なる方法を混同していると思います。引数FormBuilder#collection_selectを使用して呼び出しています。FormOptionsHelper#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_nameあなたは議論を入れようとすることになりoptionsますが、それはハッシュであるはずです、それは「方法がない」という不満mergeが由来するところです。

于 2011-08-10T07:40:44.337 に答える