Rails collection_select 不起作用,但使用 options_from_collection_for_select 进行等效选择可以

发布于 2024-11-29 04:56:49 字数 631 浏览 2 评论 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 代码中的语法,并且问题不在其他地方。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-12-06 04:56:49

我认为您混合了两种不同的 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_name 放入 options 参数中,但这应该是一个哈希,这就是关于“不”的抱怨的地方merge 方法”来自。

I think you're mixing up two different collection_select methods. You're calling the FormBuilder#collection_select using the FormOptionsHelper#collection_select arguments. Maybe you want this:

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

Or perhaps this:

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

You end up trying to put :full_name in the options argument but that's supposed to be a Hash, that's where the complaint about "no merge method" comes from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文