用于 HABTM 关系的 Rails 3 collection_select 辅助方法

发布于 2024-10-05 00:07:22 字数 556 浏览 3 评论 0原文

我有 2 个模型、会话和演示者,它们之间具有 HABTM 关系。 在创建会话页面上,我想提供一个下拉框,用户可以从中选择会话的多个演示者。 我在 _form.html.erb (用于会话)中的代码是

<%= f.label :演示者 %>
<%= collection_select(:session, :presenters, Presenter.all, :id, :name,{:include_blank => ''},{:multiple => true})%>;

然而,点击创建后,我的浏览器上收到以下错误消息: Presenter(#2176431740)预期,得到字符串(#2151988680)

请求日志显示“presenters”=>[“1”,“2”]

我猜测正在返回包含所选演示者id的字符串数组而不是演示者对象。我不明白如何让它发挥作用。

(PS-我已经创建了 Presenters_sessions 表并在两个模型中指定了 has_and_belongs_to_many)

提前致谢。

I have 2 models, sessions and presenters with a HABTM relationship between them.
On the create session page, I would like to provide a drop down box from which the user may select multiple presenters for the session.
My code in the _form.html.erb (for sessions) is

<%= f.label :presenters %>

<%= collection_select(:session, :presenters, Presenter.all, :id, :name,{:include_blank => ''},{:multiple => true})%>

However on hitting create I get the following error message on my browser:
Presenter(#2176431740) expected, got String(#2151988680)

The request log shows "presenters"=>["1","2"]

I am guessing that an array of strings containing the ids of the selected presenters is being returned instead of presenter objects. I cannot understand how to get this to work.

(PS- I have created the presenters_sessions table and specified has_and_belongs_to_many in both models)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

赏烟花じ飞满天 2024-10-12 00:07:22

如果你还没有弄清楚这一点,如果你传入 :presenter_ids 作为第二个参数而不是 :presenters ,它就会起作用。最后,您只需将选定的 id 映射到模型的 id 集合。该错误显示“您尝试将字符串分配给演示者集合”。

I you haven't figured this out, it will work if you pass in :presenter_ids as the second parameter rather than :presenters. In the end, you are just mapping the selected ids to the model's id collection. The error is saying "You tried to assign a string to a collection of Presenters".

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