2013-10-05 19 views
0
Record: 
    belongs_to :user 
    has_one :course 
    has_one :client, through: :user 
    has_one :group, through: :user 

在該記錄的索引的操作頁面形式濾波器索引我想有一個與客戶端,組和用戶collection_selects(形式的形式,我相關的collection_selects已經完成)...但我不知道如何使表單提交按鈕返回一個已過濾的索引頁面。軌道4 - 經由collection_select值在索引動作

我有範圍設置只是不知道如何從窗體調用它們。

創型號W /範圍:GitHub Link

_index_filter_form管窺:GitHub Link

記錄的Controler:GitHub Link

回答

0

我原本這是我的觀點:

<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被命名爲記錄模型中的作用域。