I am trying to make some clean URLs in a Rails3 application I am working on… but I am having a hard time understanding how to (or if I even should) customise my routes to make this work.
Here is the example:
I have a list of Stores. Each store is in a category (health, sports etc). Each store has a location.
I have 2 ways I’d like to present the data. One display is a list of all the stores in a directory type structure, the other is on a map.
Ideally I’d like my URLs to work something like this:
/stores/health/map (or /stores/map/health) to show just the health stores on a map (where essentially the map parameter is effecting which view is displayed, but still using the Index controller… which using a collection in my route doesn’t seem to suit)
The other URL I’d like is /stores/sports/ to show just the sports stores in a directory view (the default) for example…
I am not entirely clear how I can manipulate the routes to handle this…
Here is my current Route which isn’t really doing it for me:
resources :stores do
collection do
get 'map'
end
end
On top of that, I’d like to be able to add filters without using ?query=params… so:
/stores/sports/hockey , would essentially filter out only hockey stores…
I have no issues doing this with ?query, it’s just putting my params into a nicer URL that I’m trying to achieve.
The documentation does not seem to outline what I am trying to do, so Im assuming what Im trying to do is wrong.
Is this breaking REST? Am I looking at it all backwards?
Thanks for your help, JD
You might be overthinking this. 🙂
If you want to route HTTP Get of ‘stores/health/map’ to the StoresController with an action name of, say, health_map, what you need to do is:
Anything that is a clean URL and doesn’t modify data and uses HTTP GET is RESTful. (And that is coming from a co-author of a book on REST). It is when you wish to modify data that you need to be more careful on how you use methods.
To do filtering, try something like:
The value of the filter will come into your method as
params[:filter]