1

In my app, users have messages.

resources :users do
   resources :messages
end

Users can access their profile with a pretty url, say /albert.

match ':username' => 'users#show', :as => 'username'

But not their messages!

user_message_path(@user, 1)

returns /users/1/messages/1. How do I get it to return /albert/messages/1?

1 Answer 1

1

In your routes add:

get ':username/messages/:id' => 'messages#show', :as => 'username_message'

To access, use username_message_path

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but is there a more generic way of doing this? one would have to add that line for each nested resource, which becomes cubersome
I encourage you to take a look at the definition of the resource method: api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/… (click on show source). If the patterns it provides is not to your liking, you can create your own custom method tailored to your application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.