collection_select 和 has_and_belongs_to_many 关系
我有两个模型 - 联系人和组织 - 设置为 has_and_belongs_to_many 关系:
class Contact < ActiveRecord::Base
has_and_belongs_to_many :organizations
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :contacts
end
在联系人的新视图中,我有以下 collection_select:
<%= collection_select('contact', 'organization_ids', @organizations, :id, :name, {}, { :multiple => :true, :name => 'contact[organization_ids][]'}) %>
这可行,但对于为此关系创建多选框似乎过于复杂。
对于这种特殊的关系,有更好的帮手吗?我更喜欢使用多选框 - 而不是复选框。
I have two models - Contacts and Organizations - setup in a has_and_belongs_to_many relationship:
class Contact < ActiveRecord::Base
has_and_belongs_to_many :organizations
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :contacts
end
In the contact's new view, I have the following collection_select:
<%= collection_select('contact', 'organization_ids', @organizations, :id, :name, {}, { :multiple => :true, :name => 'contact[organization_ids][]'}) %>
This works, but seems overly complicated for creating a multi-select box for this relationship.
Is there a better helper available for this particular relationship? I'd prefer to use a multi-select box - not checkboxes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: