2011-09-16 2 views
0

MetaSearch를 사용하여 검색 양식으로 필터링하려는 인덱스 페이지가 Users입니다. 그러나 체크 박스를 클릭 할 때 검색하고자하는 값은 문자열로 저장됩니다.문자열에 대한 확인란에서 MetaSearch를 사용할 수 있습니까?

<% form_for(current_user.profile) do |f| %> 
<table id="careerCriteria"> 
    <tr> 
    <td class="normal"><%= current_user.profile.hometown %></td> 
    <td><%= check_box_tag :hometown %></td> 
    </tr> 
    <tr> 
    <td class="normal"><%= current_user.profile.current_city %></td> 
    <td><%= check_box_tag :current_city %></td> 
    </tr> 
    <tr> 
    <td class="normal"><%= current_user.profile.past_city %></td> 
    <td><%= check_box_tag :past_city %></td> 
    </tr> 
</table> 
<% end %> 

내 사용자 모델 : 나는 검색 버튼을 사용하지 않으

class User < ActiveRecord::Base 
    has_one :profile, :dependent => :destroy 
end 

예를 들어, 나는 여기에 수 MetaSearch을 적용 할 양식입니다. 확인란 (또는 확인란)을 클릭하면 필터가 적용되기를 원합니다. 나는 프로그래밍에 익숙하지 않아 어떤 도움을 주시면 감사하겠습니다!

답변

1

이 작업을 수행하려면 약간의 ajax & 쿼리가 필요합니다.

다음은 확인란을 선택하여 양식을 제출하는 방법을 보여주는 유용한 문서입니다. 당신이 할 수있는 것들

http://trevorturk.com/2010/08/24/easy-ajax-forms-with-rails-3-and-jquery/

는 검색을 처리하는 컨트롤러의 액션을 만드는 것입니다. 다음은 검색 작업의 예입니다 ...

def search 
    if params[:term].blank? 
     raise "You must provide search criteria." 
    end 

    params[:term] = "%#{params[:term]}%" 
    conditions = " Description LIKE :term" 

    @careers = Career.all(
     :conditions => [conditions, params], 
     :offset  => params[:offset], 
     :limit  => params[:limit] 
    ) 

    respond_with @careers 
end 

이 작업을 수행 할 경로를 설정해야합니다.

resources :careers do 
    get "search/:term/:offset/:limit.:format", :action => "search", :constraints => { :offset => /\d+/, :limit => /\d+/ } 
end 

이 작업에 제출하는 양식을 얻으면 jQuery를 사용하여 결과를 업데이트 할 수 있어야합니다.

이제 Ajax & jQuery를 사용하여 결과를로드하지 않으려면 양식 태그에서 원격 액션을 취하면 전체 페이지가 새로 고침됩니다.

+0

감사합니다.하지만 여러 검색어를 사용하여 검색하는 방법은 무엇입니까? 여러 다른 용어로 필터링 할 수있는 기회를 원합니다. 예 : 거주지, current_city, past_city. – tvalent2

+0

모델에서 범위를 사용해야합니다. 개요를 제공하는 링크가 있습니다. 그것은 또한 당신이 찾고있는 것을 정확하게하는 방법을 보여줄 수있는 이점이 있습니다 : http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny- on-scope-formerly-named-scope/index.html – Altonymous

+0

지금까지 도움을 주셔서 감사합니다. 나는 이것에 단편적인 것들을 덧붙여서 또 다른 질문을 던졌다. 당신은 내 모습을보고 그것을 연결하는 방법을 알아낼 수 있습니까? http://stackoverflow.com/questions/7466123/form-filter-on-index-page-using-cross-model-scope – tvalent2

관련 문제