views:

67

answers:

1

I'm using Rails 2. I have resources nested like this: - university_categories - universities - studies - professors - comments

I wish to use RESTful routes, but I don't want all that clutter in my URL. For example instead of: /universities/:university_id/studies/:study_id/professors/:professor_id I want: /professors/:university_id/:study_id/:professor_id (I don't map professors seperately so there shouldn't be a confusion between this and /professors/:professor_id since that route shouldn't exist). Again, I want to use RESTful resources/routes... Also note, I am using slugs instead of IDs. Slugs for studies are NOT unique, while other are. Also, there are no many-to-many relationships (so if I know the slug of a professor, which is unique, I also know which study and university and category it belongs to, however I still wish this information to be in the URI if possible for SEO, and also it is necessary when adding a new professor). I do however want to use shallow nesting for "administrator" URIs like edit, destroy (note the problem here with Study since it's slug is not unique, though)...

I would also like some tips on how to use the url helpers so that I don't have too much to fix if I change the routes in the future...

Thank you.

A: 

It doesn't seem like map.resources will provide you with this functionality, but you could use something like (untested)

map.show_professor "/professors/:university_id/:study_id/:professor_id", :controller => "professors", :action => "show"

and then similar routes for the other actions.

There might be a better solution, but this is the only way I can find that would work, since it seems map.resources assumes it is in the form of /resources/(:resource_id)

You can use REST this way, you just have to do all the actions yourself instead of using the shortcut.

As an example of an edit, you can just use

map.edit_professor "/professors/:id/edit", :controller => "professors", :action => "edit"
mathepic
That's the way I had my routes done before, but I wanted to clean up a bit. So I'm wondering if there's a cleaner way to do it, but thanks for your answer!
mrbrdo
I think you have to do this manually for now or put it into Rails itself.
mathepic