views:

36

answers:

1

I'm working with Ruby on rails 2.3.8 and the idea is to implement a "Sort" functionality for search results.

I've got the view(this is a part of it):

<span>Sort by:</span>&nbsp;&nbsp; <%=  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?

+1  A: 

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
Shripad K
I was thinking of doing something like this, but its problem is that I don't receive the collection of announcements in the params[:search](params[:announcements] in my code), but I receive their permalinks instead. For example, I get these params: {"controller"=>"announcements", "action"=>"search_filter_relevance", "announcements"=>"anuncio-3/apoyo-en-cbc/anuncio-tags-con-plugin/prueba-delta-1"} how can I get the colleciton of announcements?
Brian Roisentul
The point here, Brian, is that you should not pass the collection from the view to the controller.You should maintain the search query.For eg. if someone found the announcements typing "blah" in the textfield, then your link for ordering should pass the query again to the controller, so the controller searches again from the database and sort it.
robertokl
Ok I'll try this and let you know. Thanks
Brian Roisentul