ruby-on-railsmeta-search

how to do two search in the same page with meta_search?


I have two list in a page, one for a model_1 and another for a model_2. When I click the sort_link of a column meta_search send the param "search[meta_sort]=column_name.asc". The problem is in the controller because the two models get filtered with the same search parameters:

#in the controller
@search_for_model_1 = Model1.search(params[:search])
@model_1s = @search_for_model_1.all

@search_for_model_2 = Model2.search(params[:search])
@model_2s = @search_for_model_2.all

#in the view
<%= sort_link @search_for_model_1, :name %>
<%= sort_link @search_for_model_2, :name %>

the sort_links are in different html tables, one showing model_1 fields and the another showing model_2 fields, when I click in any column name link, the param I get in the controller is params[:search], I have no way to know if the column link was clicked from model_1 or model_2 html table.

I want change the param name "search" for something like "search_for_model_name" then in the controller:

#in the controller
@search_for_model_1 = Model1.search(params[:search_for_model_1])
@model_1s = @search_for_model_1.all

@search_for_model_2 = Model2.search(params[:search_for_model_2])
@model_2s = @search_for_model_2.all

I could not find the way to change the param name using the helper method sort_link that meta_search provide. Or is there a different manner to do this?


Solution

  • sort_link uses the :as option just like form_for

    <%= form_for @search, :as => :q do |f| %>
    
    <%= sort_link @search, :field, :as => :q
    
    @search = Model.metasearch(params[:q])
    

    So do that with different names for each model.