用于 HABTM 关系的 Rails 3 collection_select 辅助方法
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你还没有弄清楚这一点,如果你传入 :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".