HABTM 通过复选框进行编辑
我在出版物和类别之间有 HABTM 关系。在新视图和编辑视图中,我有这个:
Categories:<br />
<% @categories.each do |c| %>
<%= check_box_tag :category_ids, c.id, @publication.categories.include?(c), :name => 'publication[category_ids]' -%>
<%= "#{c.name}"%><br />
<% end -%>
模型代码:
class Publication < ActiveRecord::Base
has_many :listings
has_many :categories, :through => :listings, :order => "listings.position"
这显示得很好 - 不过,在更新时,它只保存最后一个检查的类别(如果我检查多个类别,则只保存一个类别),如果我不这样做不要选中任何框,它不会改变映射的内容。
I have a HABTM relationship between Publications and Categories. In the new and edit views, I have this:
Categories:<br />
<% @categories.each do |c| %>
<%= check_box_tag :category_ids, c.id, @publication.categories.include?(c), :name => 'publication[category_ids]' -%>
<%= "#{c.name}"%><br />
<% end -%>
The model code:
class Publication < ActiveRecord::Base
has_many :listings
has_many :categories, :through => :listings, :order => "listings.position"
This displays just fine - on update, though, it only saves the last category that is checked (if I check multiple categories, only one is saved), if I don't check any box, it doesn't change what's mapped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的问题-我必须在发布上实现一个category_ids setter。我是这样实现的:
My problem - I had to implement a category_ids setter on Publication. I implemented it as so: