Rails 5 collection_select: 在一列中显示多个属性
Rails 5 collection_select: Showing multiple attributes in one column
我正在尝试创建一个集合 select,它显示来自两个不同模型的两个属性。
我想要 select 一个帐户。该帐户有一个名称和一个所有者。所有者是一个模型,它也具有属性名称。
使用集合 select 时,我希望它显示: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>
ex: 一个帐户的名称是 Main account 并且帐户的所有者是 Stan,当 selecting 它时应该显示 Stan - 主帐户。
曾合作过:
<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.owner.name} - #{a.name}", a.id] },:second,:first %>
试试下面的代码
<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.name} - #{a.owner.name}", a.id] } %>
我正在尝试创建一个集合 select,它显示来自两个不同模型的两个属性。
我想要 select 一个帐户。该帐户有一个名称和一个所有者。所有者是一个模型,它也具有属性名称。
使用集合 select 时,我希望它显示: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>
ex: 一个帐户的名称是 Main account 并且帐户的所有者是 Stan,当 selecting 它时应该显示 Stan - 主帐户。
曾合作过:
<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.owner.name} - #{a.name}", a.id] },:second,:first %>
试试下面的代码
<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.name} - #{a.owner.name}", a.id] } %>