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 %>
我得到的错误是:
:full_name:Symbol 的未定义方法“merge”
有什么建议吗? :full_name 与工作代码一起正常工作的事实让我相信我搞砸了简化的 collection_select 代码中的语法,并且问题不在其他地方。
Here is my code snippet that works exactly as intended:
<%= f.select(:other_model_id,
options_from_collection_for_select(
OtherModel.all,
:id,
:full_name,
{ :selected => @this_model.other_model_id} )) %>
But for some reason, this doesn't work:
<%= f.collection_select :this_model, :other_model_id,
OtherModel.all, :id, :full_name %>
There error I get is:
undefined method `merge' for :full_name:Symbol
Any suggestions? The fact that :full_name works properly with the working code leads me to believe I screwed up the syntax in the simplified collection_select code and that the problem is not elsewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您混合了两种不同的
collection_select
方法。您正在调用FormBuilder#collection_select
使用
FormOptionsHelper#collection_select
参数。也许你想要这样:或者也许这样:
你最终尝试将
:full_name
放入options
参数中,但这应该是一个哈希,这就是关于“不”的抱怨的地方merge
方法”来自。I think you're mixing up two different
collection_select
methods. You're calling theFormBuilder#collection_select
using theFormOptionsHelper#collection_select
arguments. Maybe you want this:Or perhaps this:
You end up trying to put
:full_name
in theoptions
argument but that's supposed to be a Hash, that's where the complaint about "nomerge
method" comes from.