为什么脚手架生成这样的路线?他们为什么工作?
《Agile Development with Rails》一书在第二章中展示了,你可以说:
<%= link_to "Goodbye",say_goodbye_path %>
而不是将路径硬编码为“/say/goodbye”。有道理,我心想。可能 Ruby 将 say_goodbye_path
分割为 _
,将第一部分指定为控制器名称,将第二部分指定为操作名称。但是,后来,我生成了以下脚手架:
rails generate scaffold User name:string mail:string
我在 index.html.erb
视图中注意到,它具有如下方法:edit_user_path(user)
。我尝试将其重写为user_edit_path(user)
,但是当然,它不起作用。我的问题是,为什么脚手架链接是相反的?我怎么知道我是否应该按照作者在 link_to
中使用它们的方式编写它们,或者按照它们由脚手架生成的方式编写它们。你能解释一下吗?
The book "Agile development with Rails" shows in the second chapter, that you can say:
<%= link_to "Goodbye",say_goodbye_path %>
Instead of hardcoding the path to "/say/goodbye". Makes sense, I thought to myself. Probably Ruby is splitting the say_goodbye_path
by _
, assigns the first part as the controller name, the second part as the action name. But, afterwards, I generated the following scaffold:
rails generate scaffold User name:string mail:string
And I noticed in the index.html.erb
view, that it had methods like: edit_user_path(user)
. I tried to rewrite it to user_edit_path(user)
, but of course, it didn't work. My question is, why are the scaffold links the other way around? How would I know if I should write them in the way the author uses them in link_to
, or in the way they are generated by the scaffold. Can you shed some light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像
user_edit_path
这样的辅助函数是由 Rails 自动生成的,用于将对资源的操作映射到匹配的路由,从而映射到 HTTP 路径和 HTTP 动词。您必须明白,您在这里处理的是资源,而不一定是简单的控制器。虽然大多数时候您的资源可以映射到单个控制器,但事实并非必须如此。您可以拥有嵌套或组合的资源,这可能会导致相当复杂的路由定义。
资源通常是通过给它一个名称(在本例中为
user
)并定义一些允许的操作来定义的。 Rails 鼓励遵循 REST 模式,这样您就可以通过快捷方式来预定义一些操作。其中之一是edit
,默认情况下它匹配对 users_controller#edit 的 GET 请求。 RAILS 资源上的默认操作是:这些映射可以在您的routes.rb 上进行自定义(更改方法、添加或删除操作等)。通常,鼓励您使用默认映射,因为这些映射受到标准帮助程序的支持,并使您的应用程序更容易理解。
The helper functions like
user_edit_path
are automatically generated by rails to map operations on resources to the matching routes and thus HTTP paths and HTTP verbs. You have to understand that you are dealing with resources here, not necessarily with simple controllers.While most of the time your resources can map to a single controller, it doesn't have to be that way. You can have nested or combined resources which can result in rather complex routing definitions.
Resources are typically defined by giving it a name (
user
in this case) and defining some allowed operations on them. Rails encourages to follow the REST pattern there, so you can have shortcuts to have some operations pre-defined. One of them isedit
, which by default matches to a GET request to users_controller#edit. Default operations on RAILS resources are:These mappings can be customized on your routes.rb (changing methods, adding or removing operations, ...) Generally you are encouraged to use the default mappings as these are supported by standard helpers and make your app easier to understand.
脚手架是由模板生成的与路由无关的代码。
路由基于配置文件夹中的
route.rb
。所有资源均默认路由(由脚手架生成时),但您可以启用默认规则/:controller/:action/:id
。考虑一个“包罗万象”的案例。查看路由的一种方法是编辑
route.rb
并运行rake paths
并查看它们如何变化。这里也有一个官方指南:http://guides.rubyonrails.org/routing.htmlScaffolds are code generated by a template which is not related to the routing.
Routing is based on the
route.rb
in your config folder. All resources are routed by default (when generated by scaffolds) but there's a default rule/:controller/:action/:id
that you can enable. Think of a "catch all" case.One way to see what routes to have is to edit
route.rb
and runrake routes
and see how they change. There's an official guide here too: http://guides.rubyonrails.org/routing.html