I’m building my first Ruby on Rails application, and I want to do it the “Rails” way, making my application as RESTful as possible.
The application revolves around a search function, in which a user fills out a form defining what they are looking for, submits it, then pages through their results, viewing items they are interested in. From my understanding of REST, it sounds like I should be encoding my parameters into the URL for my results page(?)
If my base route is:
match "search" => "search#index"
And the base route for my search results are:
match "search/results" => "search#results"
What would be the best way of implementing routes/URLs/parameters for my search? The solution must:
- Support many parameters (30+), of which, any could be optional.
- Support pagination: there could be alot of results.
- Adhere to the “Rails way”!
Any suggestions/tips from you Rails pros would be very helpful, thanks!
For pagination: you can use a gem like
will_paginateorkaminariConcerning search parameters: Rails will put anything in the request’s query string into a
paramsvariable accessible within your controllers and views.What you’re trying to do (naming the urls search & search/results) doesn’t really align with “restful routes”. Still, this is what you’ve asked for.
will produce
To remain restful, try discerning what you’re searching for… for products, the results would be displayed using
Products#index.If you want to keep the search broad, I’d suggest
Search#newandResults#index. These latter options seem more RESTful.