在 ruby​​ on Rails 中添加和访问控制器

发布于 2024-10-04 05:48:00 字数 671 浏览 3 评论 0原文

如果我有一个控制器,如何使用新添加的方法通过 URL 访问它?

我感到困惑的原因是因为我有一条路线,

map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/

似乎我无法访问分配控制器中的任何其他方法,因为如果我这样做,

mysite.com/assignments/other_method

它会假设 other_method 是我传递的 ID进入 show 控制器,如上面的路由条目中所指定。

编辑:

我将其添加到顶部:

map.connect 'assignments/send/', :controller => "assignments", :action => "send"

现在收到此错误:

ArgumentError in AssignmentsController#show 

分配/发送的路线是任何分配控制器的第一个声明

If I have a controller, how do I access it via URL with newly added methods?

Reason I am confused is because I have a route,

map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/

It seems that I can't access any other method within the assignments controller because if i do

mysite.com/assignments/other_method

It'll assume that other_method is an ID I'm passing into the show controller, as specified in the route entry above.

Edit:

I added this to the top:

map.connect 'assignments/send/', :controller => "assignments", :action => "send"

and am now getting this error:

ArgumentError in AssignmentsController#show 

The route for assignments/send is the first declration for any of the assignments controller

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

欢你一世 2024-10-11 05:48:00

您的路由表应按此顺序排列,

map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/

map.connect 'assignments/send/', :controller => "assignments", :action => "send"

结束。

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

以作为最常见的情况

Your routing table should have it in this order

map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/

map.connect 'assignments/send/', :controller => "assignments", :action => "send"

to end with

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

as your most general case.

清晨说晚安 2024-10-11 05:48:00

只需为第二种情况指定正确的路由模式,并确保记住映射是从上到下评估的(第一个匹配被执行)。

Just specify the right route pattern for that second case and make sure you keep in mind that the mappings are evaluated from top to bottom (first match gets executed).

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