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

의 형태로 collection_select 값을 통해 필터 인덱스 나는 함께, 클라이언트, 그룹 및 사용자에 대한 collection_selects (형태와 양식을하고 싶습니다 관련 collection_selects 이미 만든) ...하지만 양식 제출 단추를 필터링 된 인덱스 페이지를 반환하는 방법을 모르겠습니다.레일 - 4의 기록에 대한 색인 작업 페이지에 index 액션

범위 설정을 통해 양식에서 호출하는 방법을 알 수 없습니다.

기록 모델 범위/w : GitHub Link

_index_filter_form 부분보기 : GitHub Link

기록 CONTROLER :

<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> 

I : GitHub Link

답변

0

원래 내보기에서와 같이이 있었다 내가 어떻게 매개 변수를 전달하는지 살펴보고 위의 코드를 수정했습니다 :

<!-- 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> 

이제 양식 제출 후 컨트롤러에서 색인 작업을 통해 매개 변수에 액세스 할 수 있습니다. by_client, by_group 및 by_user가 기록 모델에있는 범위를 지정됩니다

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 

.