在Ruby on Rails中,routes.rb中,如果map.something会创建something_path和something_url,那么map.connect也会创建类似的东西吗?

发布于 2024-09-05 13:49:37 字数 359 浏览 7 评论 0原文

在 Ruby on Rails 中,routes.rb 中,如果我们创建一个“命名路由”,

map.something ":a/:b", :controller => 'foobar'

它还会创建 something_pathsomething_url 这两个方法可在控制器和视图中使用。 map.connect 也会创建类似的东西吗?否则,map.connect 这样不是有点不利吗?我检查了 connect_pathconnect_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-09-12 13:49:37

你的想法是正确的。 map.connect 不会创建 something_pathsomething_url。这就是 map.something 等命名路由的目的:创建“名称”,因此称为“命名路由”。

You are correct in your thinking. map.connect does not create something_path and something_url. This is the purpose of named routes like map.something: To create "names," hence the name "named routes."

躲猫猫 2024-09-12 13:49:37

命名路由可以被认为是命名的map.connect路由。 map.connect 只是建立一条指向控制器内操作的路由。但到处都一遍又一遍地调用路线会很痛苦。使用命名路由更具可读性。 map.connect 的优点是它可以连接到任何控制器操作。如果仔细阅读routes.rb文件,您会发现以下两行语句具有最低优先级:

Note: These default routes make all actions in every controller accessible via GET requests. You should
consider removing or commenting them out if you're using named routes and resources.
    map.connect ':controller/:action/:id'
    map.connect ':controller/:action/:id.:format'

如果注释掉以上两行,您将无法访问除使用命名路由/资源定义的路由之外的任何路由。

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 of map.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:

Note: These default routes make all actions in every controller accessible via GET requests. You should
consider removing or commenting them out if you're using named routes and resources.
    map.connect ':controller/:action/:id'
    map.connect ':controller/:action/:id.:format'

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文