understanding rails routes: match vs root in routes.rb understanding rails routes: match vs root in routes.rb ruby-on-rails ruby-on-rails

understanding rails routes: match vs root in routes.rb


As far as I know

match '/something', :to => 'pages#something'match '/something' => 'pages#something'

are equivalent. It isn't uncommon to find more than one way to say the same thing in Rails. Shorthand notation abounds for commonly used methods. If you care, the latter is what I use and see more often.

As far as the root route is concerned, here is what is going on: root :to => 'pages#home' is mapping "/" to the home method in pages_controller.rb, as you already know. But using "pages#home" does not create the url "pages/home". All it does is tell rails what to execute when it encounters "/". That is why you need to also tell rails what to do when it encounters "pages/home". Route definitions are a one-way deal.

There is a lot more I could say, but I will try to keep my answer brief. Let me know if you need more clarification. Also, this rails guide is a great resource.


root :to => 'pages#home'

The url / will be mapped to pagescontroller home action.

/something will be the url mapping for the pagescontroller's something action


root :to => "pages#home"

is the default route, i.e. when you go to "yourdomain.com/" it routes to the home action in the pages controller.