Route alias in Rails

Ruby on-Rails

Ruby 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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVictorView Question on Stackoverflow
Solution 1 - Ruby on-RailsRyanView Answer on Stackoverflow
Solution 2 - Ruby on-RailsRyan BiggView Answer on Stackoverflow
Solution 3 - Ruby on-RailsjschorrView Answer on Stackoverflow