Route alias in Rails
Ruby on-RailsRuby on-Rails Problem Overview
I have a model stories in Rails 3.
I want to make an alias "books" for "stories" so I can have routes /books/192
instead of /stories/192
, and also that all my generated links (e.g. link_to) point to books' routes instead of stories' routes.
How can I do that?
Thanks
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
resources :stories, :path => :books
If you want to rename the path AND helper methods, then you do:
resources :stories, :path => :books, :as => :books
See: [Overriding the Named Helpers][1]
[1]: http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers ":as"
Solution 2 - Ruby on-Rails
That's why they made the path
option on match
which is also available on resources
:
resources :stories, :path => "books"
Solution 3 - Ruby on-Rails
Try something like this:
match 'books/:id' => 'books#show'
match 'books' => 'books#index'