views:

73

answers:

1

Hello, I am building an application that requires a search to go deeper than the initial level. I am using nested scopes to accomplish this and params in the URL

For example, when a user searches for "handbag" it creates a query like the following.

http://localhost:3000/junks?search=handbag&condition=&category=&main_submit=Go!

I want to add a :filter param to the end of this url and reload the page with the new url.

http://localhost:3000/junks?search=handbag&condition=&category=&main_submit=Go!&filter=lowest_price

As shown above the &filter=lowest_price is added to the query. I have already written controller code to handle this and I know it works as long as the query is like the 2nd link above.

my views code

 <div class = "filter_options">
    <p>
      <strong>Filter by:</strong>
      <%= link_to_unless_current "Recent", :filter => 'recent' %> |
      <%= link_to_unless_current "Lowest Price", :filter => 'lowest_price' %> |
      <%= link_to_unless_current "Highest Price", :filter => 'highest_price' %>
    </p>
</div>

This is how I have it currently, which works if my URL does not have a search string attached to it. Unfortunately, this will go to a http://localhost:3000/junks?filter=lowest_price even if there is a search string. I would like to know how to create my links so that they add onto the search string shown in the 2nd code example.

Additionally, if a search string with a filter is already present, it would only change that filter and resubmit the search string with the new filter. I hope I am being clear.

+1  A: 

You mean, something like:

link_to "Recent", url_for(params.merge :filter => 'recent')
Vlad Zloteanu