Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding rails routes: match vs root in routes.rb

i am following a rails tutorial from this link: http://ruby.railstutorial.org/chapters/filling-in-the-layout#code:static_page_routes

in the /config/routes.rb file, i have

SampleApp::Application.routes.draw do
  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'

  root :to => 'pages#home'
end

when i run the site, it gives me an error: no route exist pages/home. i search around the forum and ppl suggest putting match '/pages/home' => 'pages#home'

which i did:

SampleApp::Application.routes.draw do
  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/pages/home' => 'pages#home'

  root :to => 'pages#home'
end

everything works. but now, my question is, what is the difference between

1. match '/something', :to => 'pages#something'
2. match '/something' => 'pages#something'
3. root :to => 'pages#home'

basically, the code i just put. shouldn't the root takes take of the main home page and i wont' need match pages/home => pages#home?

so confusing,

Thanks!

EDIT1: I'm not getting the answers I want and so I assume my question is wrong. I'll break it down into 2 parts:

  1. What is the difference between:

    match '/pages/home' => 'pages#home' AND root :to => 'pages#home'

some say that root takes it to your root page which i can understand but as i explained above, if i just have root to: the pages/home shows a routing error. pages/home should be the same as the root page, correct?

  1. what is the difference between:

    match '/contact', :to => 'pages#contact' AND match '/pages/home' => 'pages#home

syntactically, the first line has the :to => and the 2nd line does not. is the to: needed? what does it do?

thanks

like image 536
okysabeni Avatar asked Apr 20 '11 15:04

okysabeni


People also ask

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

What is routes RB in Rails?

rb . The Rails router recognises URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. Let's consider an application to book rooms in different Hotels and take a look at how this works.

What is root route in Rails?

Root Routes Setting the Root Page of the AppThe root webpage is the one that displays for requests that include only the domain/port part of the URL and not the resource path (e.g., http://localhost:3000/). The root webpage is often thought of as the default page for the web app.

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.


1 Answers

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.

like image 150
brettish Avatar answered Oct 08 '22 10:10

brettish