views:

153

answers:

2

If I implement RESTful routing for a controller 'galleries' like as follows:

map.resources :galleries 

By default the show url for this controller would be:

/galleries/:id 

which would respond to any requests to /galleries/1 etc.

What if I had a gallery record in the database with a 'name' attribute with value 'portraits'. Could I do the same as follows:

/galleries/portraits 

instead of doing

/galleries/1 ?
A: 

Then you have to think of what happens if the gallery name is something not very URL-friendly like 'Autumn/Fall2009#1' where you have a forward slash (will change the routing) and a dash (will not be submitted to the server if left unencoded). So, you will need urlencoding when generating such URLs.

naivists
ah I see your point and now I understand why that option wasnt built-in, but im just making a simple photoblog for myself and ill be responsible for adding galleries myself, so that wasnt a concern.
udit
+2  A: 

In your Gallery model, add a #to_param method that returns what you want in the URL (in this case, name). In your controller, you still access the value through params[:id] and you'll probably have to use Gallery#find_by_name instead of Gallery#find.

As long as you use the provided helpers (gallery_path, etc.), your URLs should be pretty.

Chris Doble