问题描述
我很难理解滑轨3中的路线。我创建了两个支架:用户和杂志。用户可以登录,但是我无法链接到杂志页面。我知道这与创建路线有关。如果我通过URL导航到localhost:3000 / magazines,则可以看到我创建的多个杂志以及与每个杂志关联的每个用户。我似乎一点都没联系。我想创建一个从用户页面到杂志页面的链接。我知道这是基本知识,但是所有路线文档对我来说都没有意义。
I am having a hard time understanding routes in rails 3. I created two scaffolds: Users and magazines. The users are able to login, but I am unable to link to the magazine page. I know it has to do with creating a route. If I navigate via the URL to localhost:3000/magazines, I can see the multiple magazines I created and each user associated with each magazine. I just can't seem to connect the dots. I want to create a link from the user page to the magazine page. I know this is basic, but all the routes documentation just are not making sense to me. Thanks so much for your time.
推荐答案
以前的答案中指出的资源非常棒,这就是我的起点。我仍然提到,以防万一我被卡在某个地方。我发现在此资源中缺少的一件事是,它不包含读取路由表的说明,即命令 rake route
的输出,需要花费一些时间才能将各个部分组合在一起。尽管如果您耐心阅读整个指南,则可以将各个部分组合在一起。
Resource pointed out in previous answers is awesome and that is where I got started. I still refer that in case I am stuck somewhere. One thing I find missing in the recourse is that it doesn't include the explanation of reading the routes table i.e. output of command rake routes
and it takes time to fit the pieces together. Although if you read through the whole guide patiently, you can fit the pieces together.
在我的系统上,耙路给出以下输出(与 resources:messages
相关的摘录)
On my system 'rake routes' gives the following output (excerpt relevant to resources :messages
)
messages GET /messages(.:format) {:action=>"index", :controller=>"messages"}
POST /messages(.:format) {:action=>"create", :controller=>"messages"}
new_message GET /messages/new(.:format) {:action=>"new", :controller=>"messages"}
edit_message GET /messages/:id/edit(.:format) {:action=>"edit", :controller=>"messages"}
message GET /messages/:id(.:format) {:action=>"show", :controller=>"messages"}
PUT /messages/:id(.:format) {:action=>"update", :controller=>"messages"}
DELETE /messages/:id(.:format) {:action=>"destroy", :controller=>"messages"}
此表中的所有列均提供非常重要的信息:
All the columns in this table give very important information:
- 路线名称(第一列):这给出了路线的名称,您可以在该名称后附加 _url或 _path以得出路线的辅助名称。例如,第一个是消息,因此您可以在视图和控制器中使用
messages_path
和messages_url
作为辅助方法。查看表,您可以知道messages_path
将生成形式为 /messages(.:format)的路径。同样,生成的其他路由名称是 new_message, edit_message和 message。您还可以控制。 - HTTP动词(第二列):提供有关此路由将响应的http动词的信息。如果不存在,则表示该路由将响应所有http动词。通常,浏览器仅支持 GET和 POST动词。 Rails通过传递带有动词名称作为值的参数 _method来模拟 PUT和 DELETE,从而模拟 PUT和 DELETE。默认情况下,链接产生动词 GET,并在 POST中提交表单。与第一列结合使用,如果将
messages_path
与http GET一起使用,它将匹配第一条路线,如果与 POST一起使用,它将匹配第二条路线。需要特别注意的是,具有不同http动词的相同url可以映射到不同的路由。 - URL模式(第3列):它像有限功能的常规具有自己语法的表达式。 :id的行为类似于
(。+)
,并捕获参数 id中的匹配项,因此您可以执行params [:id] ]
并获取捕获的字符串。大括号()
表示此参数是可选的。您还可以在助手中传递这些参数以生成相应的路由。例如,如果您使用message_path(:id => 123)
,则将生成输出 / messages / 123。 - 此路由的位置(第4列):此列通常告诉控制器和相应的操作,该操作将处理与该路由匹配的请求。
- Route Name(1st Column): This gives the name of the route, to which you can append "_url" or "_path" to derive the helper name for the route. For example, first one is the "messages", so you can use
messages_path
andmessages_url
in your views and controllers as a helper method. Looking at the table you can tellmessages_path
will generate a path of form "/messages(.:format)". Similarly, other route names generated are "new_message", "edit_message" and "message". You can also control the naming of routes. - HTTP Verb(2nd Column): This gives the information about the http verb which this route will respond to. If it is not present, then it means this route will respond to all http verbs. Generally browsers only support, "GET" and "POST" verbs. Rails simulate "PUT" and "DELETE" by passing a parameter "_method" with verb name as value to simulate "PUT" and "DELETE". Links by default result in a "GET" verb and form submissions in "POST". In conjunction with the first column, if you use
messages_path
with http "GET" it would match first route and if you use it with "POST" it will match second route. This is very important to note, same url with different http verbs can map to different routes. - URL Pattern(3rd Column): Its like a limited featured regular expression with syntax of its own. ":id" behaves like
(.+)
and captures the match in parameter "id", so that you can do something likeparams[:id]
and get the captured string. Braces()
represent that this parameter is optional. You can also pass these parameters in helpers to generate the corresponding route. For example if you usedmessage_path(:id => 123)
is will generate the output "/messages/123". - Where this routes(4th Column): This column generally tells the controller and the corresponding action which will handle requests matching this route. There can be additional information here like constraints if you defined any.
因此,如果 localhost:3000 / magazines是您想要的页面,则这里可以有其他信息,例如约束。 ,您应该使用url模式作为 /magazines(.:format)来检查路由表,并亲自进行剖析以找出所需的内容。如果您只是开始使用Rails,我建议您从上至下阅读整个。
So if "localhost:3000/magazines" is the page you want, you should check the routes table with url pattern as "/magazines(.:format)" and disect it yourself to find out what you need. I would recommend you read the whole guide from top to bottom if you are just starting rails.
(在这里写所有这些可能只是一个矫kill过正,但是由于此信息无法以统一的方式提供,所以我遇到了很多问题。总是想将其写出来并我希望可以在 http://edgeguides.rubyonrails.org/routing.html在单独的部分中。)
(This might be just an overkill to write all this here, but I faced many problems due to this info not being available in a consolidated manner. Always wanted to write it out and finally did. I wish it was available on http://edgeguides.rubyonrails.org/routing.html in a separate section.)
这篇关于红宝石在铁轨路线上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!