Giving a nested route an alias in Rails
-
09-09-2019 - |
Question
If I want to provide an alias for a controller, I can use map.resources :rants, :controller => 'blog_posts'
yoursite.com/rants points to the blog_posts
controller fine.
How do I give an alias to a nested resource, for example yoursite.com/users/5/rants ?
Solution
You may want to try:
map.resources :rants, :controller => 'blog_posts'
map.resources :users do |users|
users.resources :rants, :controller => 'blog_posts'
end
This will give you the yoursite.com/users/5/rants/
url that you are looking for and it will generate the handy methods (for example: users_rants_path(@user)
)
Hope this helps.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow