在Ruby on Rails中,routes.rb中,如果map.something会创建something_path和something_url,那么map.connect也会创建类似的东西吗?
在 Ruby on Rails 中,routes.rb 中,如果我们创建一个“命名路由”,
map.something ":a/:b", :controller => 'foobar'
它还会创建 something_path
和 something_url
这两个方法可在控制器和视图中使用。 map.connect
也会创建类似的东西吗?否则,map.connect
这样不是有点不利吗?我检查了 connect_path
和 connect_url
都不是自动创建的。
In Ruby on Rails, routes.rb, if we create a "named route"
map.something ":a/:b", :controller => 'foobar'
it will also create something_path
and something_url
which are two methods usable in the controller and in the view. Does map.connect
create something like that too? Otherwise, isn't map.connect
somewhat disadvantaged in this way? I checked that connect_path
and connect_url
both aren't created automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的想法是正确的。
map.connect
不会创建something_path
和something_url
。这就是map.something
等命名路由的目的:创建“名称”,因此称为“命名路由”。You are correct in your thinking.
map.connect
does not createsomething_path
andsomething_url
. This is the purpose of named routes likemap.something
: To create "names," hence the name "named routes."命名路由可以被认为是命名的
map.connect
路由。map.connect
只是建立一条指向控制器内操作的路由。但到处都一遍又一遍地调用路线会很痛苦。使用命名路由更具可读性。map.connect
的优点是它可以连接到任何控制器操作。如果仔细阅读routes.rb文件,您会发现以下两行语句具有最低优先级:如果注释掉以上两行,您将无法访问除使用命名路由/资源定义的路由之外的任何路由。
A named route can be thought of as a named
map.connect
route.map.connect
just establishes a route that points to an action within a controller. But it would be a pain to call the route again and again everywhere. Using a named route is more readable. The advantage ofmap.connect
is that it can be made to connect to any controllers action. If you read the routes.rb file carefully you see that the following two statements have the lowest priority:If you comment out the above two lines you will not be able to reach any route except for the ones that you define using named routes/resources.