【发布时间】:2012-03-20 21:22:01
【问题描述】:
我有这行代码:
<%= select_tag :friendship_id, options_from_collection_for_select(current_user.friendships, "id", "name", selected = nil) %>
在“朋友”表中选择记录的“名称”并将其作为选项放在 select_tag 上
Friendships 有一个friendship_id 链接到friends 表。我用
来称呼这个名字def name
self.friend.name
end
在友谊控制器中
该关联有效,因为我在网页上看到了姓名列表。
我想:
- 按字母顺序排列
- 在列表顶部添加“选择朋友...”
我还没有真正找到任何东西。 对于问题 2,我添加 selected = nil 无济于事。
提前感谢您的帮助
编辑(回答):
我最终改变了收集收藏的方式,并寻找朋友而不是友谊。
<%= select_tag :friend_id, options_from_collection_for_select(current_user.friends, "id", "name"), :prompt => 'Select a friend...', :id => 'thought_contact_select' %>
在我的用户模型中
has_many :friends, :through => :friendships, :order => :name
我只是在控制器上查找与该朋友和该用户关联的友谊
friendship = Friendship.find_by_user_id_and_friend_id(current_user.id, params[:friend_id])
【问题讨论】:
标签: ruby-on-rails forms activerecord html-select