我原本這是我的觀點:
<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>
現在我可以在表單提交後通過控制器上的索引操作訪問params。
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被命名爲記錄模型中的作用域。