开发者

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',    :开发者_StackOverflow社区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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜