69

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

Victor
  • 23,172
  • 30
  • 86
  • 125

3 Answers3

125

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

Rimian
  • 36,864
  • 16
  • 117
  • 117
Ryan
  • 6,432
  • 7
  • 40
  • 54
113

That's why they made the path option on match which is also available on resources:

resources :stories, :path => "books"
Turadg
  • 7,471
  • 2
  • 48
  • 49
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • 1
    I'm not sure this is right. See [link](http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers) This only changes helpers names. Run `rake routes` after inserting that line of code. It won't give what he is asking. – Ryan Mar 13 '11 at 16:08
  • 12
    I believe Ryan Bigg is correct. However, there is a slight ambiguity in OP's question. When you say alias to a resource, it generally means that that resource is available via multiple routes. Thus, if you wanted your "stories resource" to be available via both "/stories" and "/books", then declaring something like this is also possible: resources :stories, :as => :books resources :books, :controller => "stories" Thus, the "users of your app" get the same resource (stories) via two different routes ("/books", "/stories"). – Kedar Mhaswade Nov 23 '11 at 18:45
  • 3
    I know it's old, but I really don't understand how this answers the question. I can't get it to do anything of the sort. For me it just modifies the helper methods. – Mike Campbell Feb 03 '13 at 13:21
  • 1
    @MikeCampbell: Sorry, it was wrong. The correct option to use is `:path`. – Ryan Bigg Feb 03 '13 at 21:15
-2

Try something like this:

match 'books/:id' => 'books#show'
match 'books' => 'books#index'
jschorr
  • 3,034
  • 1
  • 17
  • 20