Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route alias in Rails

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

like image 373
Victor Avatar asked Dec 21 '10 20:12

Victor


People also ask

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

What is routes in Ruby on Rails?

The routing module provides URL rewriting in native Ruby. It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules.

What is namespace in Rails routes?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.


1 Answers

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

like image 83
Ryan Avatar answered Oct 07 '22 01:10

Ryan