Kenji Crosland
Kenji Crosland

Reputation: 3034

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 to 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.

Upvotes: 2

Views: 608

Answers (2)

jonnii
jonnii

Reputation: 28312

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'

Upvotes: 2

MBO
MBO

Reputation: 30985

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)

Upvotes: 3

Related Questions