Rails Resources:   :as   vs   :controller

1:10 PM / Comments (0) / by Nick

As you may or may not know, when configuring routes and resources in config/routes.rb , you can pass the :as or :controller options to map.resources . When I first read about these two options in the Rails Routing From The Outside In Rails Guide, they seemed to do the exact same thing.

However, that's not the case at all.
map.resources :photos, :as => 'images'
This allows you to request the "photos" resource using the word "images". IE: http://localhost:3000/images/ would route to the "photos" controller.

On the other hand,
map.resources :photos, :controller => 'images'
This allows you to request the "images" resource using the word "photos". IE: http://localhost:3000/photos/ would route to the "images" controller.

Simple? Yes. Subtle? Yes.

0 comments: