【问题标题】:collection_select and has_and_belongs_to_many Relationshipscollection_select 和 has_and_belongs_to_many 关系
【发布时间】:2011-04-21 18:55:18
【问题描述】:

我有两个模型 - 联系人和组织 - 设置为 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][]'}) %>

这可行,但为这种关系创建多选框似乎过于复杂。

对于这种特殊的关系是否有更好的帮手?我更喜欢使用多选框 - 而不是复选框。

【问题讨论】:

    标签: ruby-on-rails view has-and-belongs-to-many


    【解决方案1】:

    试试这个:

    <%= check_box_tag "contact[organization_ids][]", organization.id, @contact.organizations.include?(organization) %>
    

    【讨论】:

    • 嘿@Jatin - 我很欣赏这个答案,但复选框不是这个特定项目的选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多