Ruby on rails Rails集合\u使用联接表选择
我的桌子: 个人属于和 房间里有很多人 我的收藏Ruby on rails Rails集合\u使用联接表选择,ruby-on-rails,Ruby On Rails,我的桌子: 个人属于和 房间里有很多人 我的收藏 <%= collection_select(:inspection, :person_id, Person.involving(current_user), :id, :room_id) %> 我的看法 <%= form_for @inspection do |f| %> <label>Select</label> <%= collection_select(:i
<%= collection_select(:inspection, :person_id, Person.involving(current_user), :id, :room_id) %>
我的看法
<%= form_for @inspection do |f| %>
<label>Select</label>
<%= collection_select(:inspection, :person_id, Person.involving(current_user), :id, :room.id) %>
<%= f.submit "Send" %>
挑选
我尽量把它说清楚。考虑使用
select
而不是collection\u select
。此方法允许您对每个选项的id和名称使用的内容进行更精细的控制
在您的情况下,您可能需要以下内容:
选择(:inspection,:person_id,person.included(当前_用户)。收集{{p.id,p.room.name]},{…其他方法选项…})
这允许您指定选项id/名称对的数组。让所有使用范围的人参与进来,然后迭代那些为每个找到的人返回一个数组的人,其中数组中的第一个值是选项值,第二个值是显示的选项文本
查看此项了解更多详细信息。在您的收藏中,选择要调用的
:room.id
作为填充选项名称的方法。您是否尝试过将其更改为:room.name
?实际上是:room\u id。。我更新了它。
<select name="post[person_id]">
<option value="<%= person.id %>"><%= room.name %></option>
<% end %>
</select>
scope :involving, -> (user) do
where("persons.id = ?", user.id)
end
<%= form_for @inspection do |f| %>
<label>Select</label>
<%= collection_select(:inspection, :person_id, Person.involving(current_user), :id, :room.id) %>
<%= f.submit "Send" %>