JuanPablo
JuanPablo

Reputation: 24754

rails : routes in nested resources

I use nested resources

#route.rb
resources :users do
     resources :posts
end

and with

#route.rb
match '/:username' => 'users#show', :as => :user

I change /user/id to /username

but, how change /users/username/posts to username/posts ?

Upvotes: 1

Views: 246

Answers (2)

JuanPablo
JuanPablo

Reputation: 24754

this work with

#route.rb
match '/:username/posts' => 'posts#index', :as => :user_posts

Upvotes: 0

Matt Polito
Matt Polito

Reputation: 9756

:id is really just a reference to they key that will be used to find your object. It doesn't necessarily need to be the integer representation.

Take a look at the friendly_id gem to see how slug handling is done and how to tell ActiveRecord to use that instead of the integer it expects by default.

Upvotes: 1

Related Questions