views:

100

answers:

1

I have an index action and I would like the user to be able to filter the results on the index page by clicking on "filter links" on the same page.

There is a nice railcasts video that allows you to filter results by typing into a search box.

I would like to filter results based on links the user clicks. How can I do this? Is there a way I can pass params to the controller using link_to?

for example, on an index page listing books, I would like to display the following link

<%= link_to "Fiction", books_path %>

and clicking the link would then only display books in the fiction category.

+1  A: 

Take a look at the link_to section "link_to can also produce links with anchors or query strings:"

So you example would look like (assuming a "category" filter):

<%= link_to "Fiction", books_path(:category => "fiction") %>
marklai
I tried this, but I cant seem to access params[:category] in the controller.params[:category].inspect returns nil
Maulin
oops... updated the code example. The category is passed in to books_path. You should see the generated link containing "category=fiction" in the form "/books?category=fiction".
marklai
That worked, The correct syntax is this<%= link_to "Fiction", books_path(:category => "fiction) %>Now params[:category] will be available in the controller.Thanks!
Maulin