【问题标题】:Rails 5 collection_select: Showing multiple attributes in one columnRails 5 collection_select:在一列中显示多个属性
【发布时间】:2016-06-15 06:30:06
【问题描述】:

我正在尝试创建一个集合选择,它显示来自两个不同模型的两个属性。

我想选择一个帐户。该帐户具有名称和所有者。所有者是一个模型,它也具有属性名称。 使用集合选择时,我希望它显示:account.name + owner.name。这是目前我拥有的 collection_select,它只显示 account.name

  <div class="field">
    <%= f.label :to_account_id %>
    <%= f.collection_select :to_account_id, Account.all, :id, :name %>
  </div>

例如:帐户名称为 Main account,并且该帐户的所有者是 Stan,选择时应显示 Stan - Main account .

曾与:

    <%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.owner.name} - #{a.name}", a.id] },:second,:first %>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 collection-select


    【解决方案1】:

    试试下面的代码

     <%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.name} - #{a.owner.name}", a.id] } %>
    

    【讨论】:

    • 谢谢,成功了!只需要稍微编辑代码:)
    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 2016-02-02
    • 2015-02-12
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多