Rails nested singleton routing

208 views Asked by At

I have a basic User class, and each user has a number of has_one associations with other objects (account, profile, etc). I have my routes nested as such:

resources :users do
  resource :account
  resource :profile
end

And that gives me the appropriate routes:

        user_profile POST   /users/:user_id/profile(.:format)      profiles#create
    new_user_profile GET    /users/:user_id/profile/new(.:format)  profiles#new
   edit_user_profile GET    /users/:user_id/profile/edit(.:format) profiles#edit
                     GET    /users/:user_id/profile(.:format)      profiles#show
                     PATCH  /users/:user_id/profile(.:format)      profiles#update
                     PUT    /users/:user_id/profile(.:format)      profiles#update
                     DELETE /users/:user_id/profile(.:format)      profiles#destroy

But I want the logged-in user to be able to access their owned objects via URLs like this:

/settings/profile
/settings/account
...etc

How do I need to set up my routes for that?

1

There are 1 answers

0
RustyToms On BEST ANSWER

Use this code in your routes.rb:

resources :users
resource :account, path: 'settings/account'
resource :profile, path: 'settings/profile'