最初我认为这是我的观点:
<td><%= collection_select(:client_id, 0, Client.find(:all, :order => "name"), :id, :name, {}, {:class=>'form-control'}) %></td>
<td><%= collection_select(:group_id, 0, Group.find(:all, :order => "name"), :id, :name, {}, {:class=>'form-control'}) %></td>
<td><%= collection_select(:user_id, 0, User.find(:all, :order => "first_name, last_name"), :id, :full_name, {}, {:class=>'form-control'}) %></td>
我回去看看我是如何传递参数的......并修改了上面的代码:
<!-- collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) -->
<td><%= collection_select(:client, :id, Client.find(:all, :order => "name"), :id, :name, {}, {:class=>'form-control'}) %></td>
<td><%= collection_select(:group, :id, Group.find(:all, :order => "name"), :id, :name, {}, {:class=>'form-control'}) %></td>
<td><%= collection_select(:user, :id, User.find(:all, :order => "first_name, last_name"), :id, :full_name, {:multiple => true, :size => 5}, {:class=>'form-control'}) %></td>
现在我可以在表单提交后通过控制器上的 index 操作访问参数。
def index
@records = Record.all
if params[:commit] == "Filter"
@records.by_client(params[:client_id]).by_group(params[:group_id]).by_user(params[:user_id])
end
end
其中 by_client、by_group 和 by_user 是位于记录模型中的命名范围。