Question

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?

Was it helpful?

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.

OTHER TIPS

most likely you just do something like this

:search_for_model_2 => :search_field

but I can't say for sure without seeing your view.

graywh's answer is not working for sort_link, this is the correct solution:

#in the controller
@search_for_model_1 = Model1.search(params[:model_1], :search_key => :model_1)

@search_for_model_2 = Model2.search(params[:model_2], :search_key => :model_2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top