简体   繁体   English

route.rb和控制器

[英]routes.rb and controller

Sorry, I thought I understood this, but now I have to re-evaluate my understanding of routes.rb. 抱歉,我以为我了解这一点,但是现在我必须重新评估对route.rb的理解。 Hoping you could help. 希望您能提供帮助。

A browser request goes to the Application Controller and the Controller tells what to show, right? 浏览器请求转到应用程序控制器,控制器告诉显示什么,对吧? - what erb file, database stuff, whatever... -什么erb文件,数据库内容等等

In my routes.rb file I have: 在我的routes.rb文件中,我有:

 root :to => 'static_pages#FAQ'

Until lately I thought what was happening was: routes.rb is looking at my static_pages_controller.rb file, looking at the FAQ method, and then seeing what to do. 直到最近我还以为发生了什么:routes.rb在查看我的static_pages_controller.rb文件,在查看FAQ方法,然后查看该怎么做。 If there's nothing in the FAQ method - as is the case - then Rails does its magic and goes to my FAQ.html.erb in my View, the closest thing. 如果FAQ方法中没有任何内容(通常是这样),那么Rails会发挥作用,然后转到我的View(最接近的内容)中的FAQ.html.erb。

But even if I change the name of: 但是,即使我更改了名称:

def FAQ
end

in my controller, or delete the static_pages_controller.rb altogether, it still goes to my FAQ.html.erb file. 在我的控制器中,或完全删除static_pages_controller.rb,它仍然转到我的FAQ.html.erb文件。 So does routes.rb not even look at controllers? 那么,routes.rb甚至不看控制器吗? Does it go straight to 'View' files? 是否直接进入“查看”文件? Thanks for any help. 谢谢你的帮助。

static page are served first so that is why FAQ.html.erb is always served. 首先提供静态页面,因此始终提供FAQ.html.erb的原因。

Also "A browser request goes to the Application Controller and the Controller tells what to show, right? - what erb file, database stuff, whatever..." 另外,“浏览器向应用程序控制器发送请求,控制器告诉显示的内容,对吗?-什么是erb文件,数据库内容,等等...”

I think of it this way: The request first goes through routing, then to the controller for the resource in question, the controller being inherited from application_controller, will then query the model as needed, calculating variables as needed which it then uses in comiling the View page, which are compilation, gets sent as HTML. 我这样想:请求首先经过路由,然后到达有问题的资源的控制器,该控制器从application_controller继承,然后根据需要查询模型,根据需要计算变量,然后将其用于编译查看页面(即编译页面)以HTML格式发送。

Your ideas are essentially correct; 您的想法本质上是正确的; Rails will attempt to render the static_page's controller's FAQ method, which implicitly renders the "FAQ" view, if no view (or other output) is explicitly rendered by the action. Rails将尝试呈现static_page控制器的FAQ方法,如果该操作未显式呈现任何视图(或其他输出),则该方法将隐式呈现“ FAQ”视图。

What you're missing is that Rails will fill in the blanks if any one of the controller/action pieces is missing. 您所缺少的是,如果缺少任何控制器/动作片,Rails会填补空白。 All you need to do is define the view and Rails will assume that you're simply not bothering to define an empty action. 您所需要做的就是定义视图,Rails会假设您根本就不用费心定义一个空操作。

Starting a method name with a capital letter seems like a very bad idea in ruby, because ruby will treat any identifiers that start with a capital letter as a constant. 在ruby中,以大写字母开头的方法名称似乎是一个非常糟糕的主意,因为ruby会将以大写字母开头的所有标识符视为常量。 The very first thing I would do personally is change 'FAQ' to 'faq' in my routes and controller code. 我个人要做的第一件事是在我的路线和控制器代码中将“ FAQ”更改为“ faq”。 If you really want to have 'FAQ' capitalized in the browser url, you can probably accomplish that via something like: 如果您确实想在浏览器网址中使用大写的“ FAQ”,则可以通过以下方式实现:

root :to => static_pages#faq, :as => 'FAQ'

To check that your method is getting called, use the logger: 要检查您的方法是否被调用,请使用记录器:

def faq
   Rails.logger.warn "faq method did get called after all"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM