views:

161

answers:

2

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.

+2  A: 

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'
jonnii
Not really a big problem, just wanted something other than /postname/comment/new. How would this link be represented in the view by the way?
Kenji Crosland
What's wrong with /postname/comment/new?
jonnii
Nothing wrong with it really. Just didn't want long trailing urls.
Kenji Crosland
Well /comments is shorter than /shoutouts =) Either way, I'd try not to spend too much time on URIs unless it's to improve your SEO as 99% of users don't even notice them.
jonnii
Yeah, I'll probably leave it alone unless someone says something.
Kenji Crosland
in future, if you wanted you can always do, this `map.resources :as => 'shoutouts'`, this will make the route appear as `/shoutouts/new` instead of `/comments/new`.
jonnii
+3  A: 
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)

MBO
To me, this is the most 'hygienic' approach.
lsdr