views:

39

answers:

1

Hello,

I'm new to rails and having trouble figuring out how to do this RESTfully. I have a route set as map.resources :products and what I want to do is have some way to filter the results returned from the index action. For example, you go to /products which calls the index action per the resources default, then on that page there is a filter form on the left side. You may check certain filter options and then submit this form, which would then just render basically the same page, but with filtered results (i.e. everything that costs less than $X).

Originally I was thinking to just check the HTTP method in the products controller's index method, if get, display as normal, if post, apply filter. However, this is not RESTful and I would like to figure out a RESTful way to do this, which is also elegant (code-wise). Thank you for the help.

I guess I could use a query string, but I would prefer to keep the url clean as once it's working normally I'd like to use some AJAX to update the list on the fly.

+3  A: 

This is precisely what query parameters are for. You are still using the same REST action, i.e. an index fo products. For you example, the URL should be

/products?max_cost=x

In your controller, your index action should just look at params[:max_cost] or whatever other filters you want to support, to modify the query and display the results.

fullware
Great suggestion fullware, unfortunately I'd prefer to keep the url clean as I will be using AJAX to update on the fly once it's working normally.
MAP
I'm not sure how what you suggest is possible. This is REST. If you want the URL in the *browser* to stay "clean", hit the filtered URL via AJAX to fill in the index after the page loads.
fullware