开发者

Nested named routes in rails?

Lets say you have a two models with blog posts and comments set up like this:

class post 
has_many :comments

and the routing was set up pretty much the same way:

map.resources :posts, :has_many => :comments

When I go to make a new comment it shows up as localhost::3000/postname/comments/new

What should you do in order to make the url read something like: localhost::3000/postname/shoutout ?

The reason I want t开发者_如何学Co do this is because this particular page will have more than just a new comment form on it.

I have no trouble naming routes but I'm having trouble figuring out what to do with a nested one.


map.resources :posts, :has_many => :comments, :collection => {:shoutout => :get}

Key feature is :collection, which points of pairs: 'name' => 'method', and you need to implement this name in controller (and views)


The routes have nothing to do with the forms that are on the page, I'm not sure what the problem is?

If you want to have /postname/shoutout go to to CommentsController#new you'll need to map the route manually:

map.connect '/:post_id/shoutout', :controller => 'comments', :action => 'new'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜