How to pass a collection to a controller's action(not by GET)
I'm working with Ruby on rails 2.3.8 and the idea is to implement a "Sort" functionali开发者_JAVA百科ty for search results.
I've got the view(this is a part of it):
<span>Sort by:</span> <%= link_to 'MORE RELEVANT', search_filter_relevance_path %>
Routes file:
map.search_filter_relevance "/anuncios/search_filter_relevance", :controller => 'announcements', :action => 'search_filter_relevance'
and the controller's action(doing nothing so far):
def search_filter_relevance
raise params.inspect
end
As its a search of announcements, I'd like to pass the collection of its results to the controller's action so it filters them, and not all the announcements.
How can I do that?
Your question is kind of incomplete. It would have been great if you could provide the details of the entire controller code. Still i will try answering it.
A better approach would be to pass the "search term" itself. Say the search term was stored in an instance variable @search
.
Your link_to should be:
<%= link_to 'MORE RELEVANT', search_filter_relevance_path(:search => @search) %>
And your route should be:
map.search_filter_relevance "/anuncios/search_filter_relevance/:search", :controller => 'announcements', :action => 'search_filter_relevance'
and your action:
def search_filter_relevance
#You can access the search term using params[:search] and then reproduce the search results in a filtered form!
#Example: If you using acts_as_solr to search your table(s).. (Can be applied to any other FTS plugin)
#This sorts the results in ascending order
#@result = Model.find_by_solr(params[:search], :order => "created_at ASC")
end
精彩评论