有谁知道将 activerecord 结果集的内容显示为选择框的更好方法。
我想这样做
@users = User.all
<%= f.select_box :users, options_for_select(@users) %>
但是现在我需要将所有用户解析成一个带有子数组 [username,user_id] 的多维数组
有任何想法吗?
有谁知道将 activerecord 结果集的内容显示为选择框的更好方法。
我想这样做
@users = User.all
<%= f.select_box :users, options_for_select(@users) %>
但是现在我需要将所有用户解析成一个带有子数组 [username,user_id] 的多维数组
有任何想法吗?
我一般用collection_select
<%= f.collection_select :user_id, User.all, :user_id, :username, :prompt => true %>
参考:http ://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M002303