Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...