0

I want a routes like:

get '/posts/new'           => 'posts#new'
get '/:username/posts/:id' => 'posts#show'
get '/:username/posts'     => 'posts#index'

Accessible via helpers like:

new_post_path    #=> new   -
post_path(post)  #=> show  - :username implied from post
posts_path       #=> index - :username implied from session
posts_path(user) #=> index - :username explicit

I'd like to do this with resourceful semantics, instead of specifying each route by hand. Also I'm not sure how to make the url helpers smart.

Thoughs?

Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173

1 Answers1

0

I'm assuming what you want is to have nested routes. This should get you closer to what you're looking for.

in your routes.rb file:

resources :users do
  resoures :posts
end

This will make paths like this:

/users
/users/:user_id
/users/:user_id/posts
/users/:user_id/posts/:id

Then you can tweak your routes from there so that /posts/new points to your posts controller with something like this:

(untested and not sure this is 100% correct so someone please chime in)

resources :users do
  resoures :posts do
    match "/posts/new", :to => "posts#new"
  end
end
Catfish
  • 18,876
  • 54
  • 209
  • 353
  • I want atypical nested routes. Rather than `/users/:user_id/posts`, I want `/:username/posts`. That's what's throwing me off. – Dane O'Connor Apr 04 '13 at 20:26
  • Sorry i copied the wrong thing. See updated answer. I think this is where the `match` comes in. – Catfish Apr 04 '13 at 20:28