通过“link_to”应用搜索过滤器

时间:2016-10-13 05:21:32

标签: ruby-on-rails

所以我的索引视图中有一个完美的搜索过滤器。控制器中的代码如下

def index
    @tutor = Tutor.where(:admin => false)
    @tutor_array = []

@tutor_array << @tutor.fees_search(params[:fees_search]) if params[:fees_search].present?
@tutor_array << @tutor.subject_search(params[:subject_search]) if params[:subject_search].present?
@tutor_array << @tutor.lssubject_search(params[:lssubject_search]) if params[:lssubject_search].present?
@tutor_array << @tutor.ussubject_search(params[:ussubject_search]) if params[:ussubject_search].present?
@tutor_array << @tutor.jcsubject_search(params[:jcsubject_search]) if params[:jcsubject_search].present?

@tutor_array.each do |tutor|
  ids = @tutor.merge(tutor).map(&:id)
  @tutor = Tutor.where(id: ids)
end
@tutor = @tutor.sort_by { |tutor| tutor.rating.rating }.reverse
@tutor = @tutor.paginate(:page => params[:page], :per_page => 2)

在我看来,为我搜索过滤器的表单是

<form class='form-inline'>
    <%= form_tag(tutors_path, method: :get) do %>
    <div class='row', id='filter-form'>
      <div class='form-group'>
        <%= label_tag 'subject_search', 'Primary Subject' %>
        <% subject_array = Subject.all.map { |subject| [subject.name] } %>
        <%= select_tag 'subject_search', options_for_select(subject_array, :selected => params[:subject_search]), :include_blank => true, class:'form-control' %>
        <%= label_tag 'lssubject_search', 'Lower Sec Subject' %>
        <% lssubject_array = Lssubject.all.map { |lssubject| [lssubject.name] } %>
        <%= select_tag 'lssubject_search', options_for_select(lssubject_array, :selected => params[:lssubject_search]), :include_blank => true, class:'form-control' %>
        <%= label_tag 'ussubject_search', 'Upper Sec Subject' %>
        <% ussubject_array = Ussubject.all.map { |ussubject| [ussubject.name] } %>
        <%= select_tag 'ussubject_search', options_for_select(ussubject_array, :selected => params[:ussubject_search]), :include_blank => true, class:'form-control' %>
      </div>
    </div>
    <div class='row', id='filter-form2'>
      <div class='form-group'>
        <%= label_tag 'jcsubject_search', 'JC Subject' %>
        <% jcsubject_array = Jcsubject.all.map { |jcsubject| [jcsubject.name] } %>
        <%= select_tag 'jcsubject_search', options_for_select(jcsubject_array, :selected => params[:jcsubject_search]), :include_blank => true, class:'form-control' %>
        <%= label_tag 'fees_search', 'Max Fees' %>
        <%= select_tag 'fees_search', options_for_select((10..150).step(10), :selected => params[:fees_search]), :include_blank => true, class:'form-control' %>
        <%= submit_tag 'Filter', class: 'btn btn-primary btn-xs' %>
      </div>
    </div>
    <% end %>
    <div id='filter-reset'>
      <%= link_to 'Reset Filters', tutors_path, class: 'btn btn-primary btn-xs' %>
    </div>
  </form>

在示例中传递过滤器或获得的URL如下

/tutors?utf8=✓&subject_search=Science&lssubject_search=&ussubject_search=&jcsubject_search=&fees_search=&commit=Filter

我想问的是,如何通过按钮传递过滤器?

enter image description here

所以,如果我点击“数学”或“英语”,它相当于通过表格传递过滤器?我理解它很可能是link_to tutors_path(???)但是应该在括号中输入正确的过滤器?

1 个答案:

答案 0 :(得分:0)

我试过了<%= link_to subs.name, tutors_path(:subject_search => subs.name %>,看起来它很有效。在这里提供一些上下文的视图以及subs.name的出现方式。

<% tutor.subjects.each do |subs| %>
  <span class='badge'id='tutor-listing-badge'>
    <%= link_to subs.name, tutors_path(:subject_search => subs.name) %>
  </span>
<% end %>