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 的未定义方法“合并”

有什么建议么?:full_name 与工作代码一起正常工作的事实让我相信我搞砸了简化的 collection_select 代码中的语法,并且问题不在其他地方。

4

1 回答 1

3

我认为你混合了两种不同的collection_select方法。您正在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_nameoptions但这应该是一个哈希,这就是关于“没有merge方法”的抱怨的来源。

于 2011-08-10T07:40:44.337 回答