了解 Rails 路线:在 routes.rb 中匹配与根 [英] understanding rails routes: match vs root in routes.rb

查看:34
本文介绍了了解 Rails 路线:在 routes.rb 中匹配与根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注此链接中的 Rails 教程:http://ruby.railstutorial.org/chapters/filling-in-the-layout#code:static_page_routes

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

在/config/routes.rb 文件中,我有

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

当我运行该站点时,它给了我一个错误:页面/主页不存在路由.我在论坛上搜索,ppl 建议匹配 '/pages/home' => 'pages#home'

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'

我所做的:

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'

基本上,我刚刚放的代码.根不应该占用主主页,我不需要匹配 pages/home => 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?

好纠结,

谢谢!

我没有得到我想要的答案,所以我认为我的问题是错误的.我将把它分成两部分:

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. 有什么区别:

  1. What is the difference between:

匹配'/pages/home' => 'pages#home'和根:to => 'pages#home'

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

有人说 root 将它带到我可以理解的根页面,但正如我上面解释的那样,如果我只有 root:页面/主页显示路由错误.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. 有什么区别:

  1. what is the difference between:

匹配 '/contact', :to => 'pages#contact'和匹配 '/pages/home' => 'pages#home

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

在语法上,第一行有 :to => 而第二行没有.to: 需要吗?它有什么作用?

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

谢谢

推荐答案

据我所知

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

是等价的.在 Rails 中找到不止一种表达同一件事的方式并不少见.常用方法的速记符号比比皆是.如果你在意的话,后者是我经常使用和看到的.

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.

就根路由而言,这是发生了什么:root :to =>如您所知,pages#home" 正在将/"映射到 pages_controller.rb 中的 home 方法.但是使用pages#home"不会创建urlpages/home".它所做的只是告诉 rails 在遇到/"时执行什么.这就是为什么您还需要告诉 rails 在遇到pages/home"时要做什么.路线定义是一种单向交易.

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.

我想说的还有很多,但我会尽量保持我的回答简短.如果您需要更多说明,请告诉我.此外,这个 rails 指南是一个很好的资源.

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.

这篇关于了解 Rails 路线:在 routes.rb 中匹配与根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆