开发者

REST Rails 2 nested routes without resource names?

I'm using Rails 2. I have resources nested like this: - university_categories - universities - studies - professors - comm开发者_如何转开发ents

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.


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜