在 ruby on Rails 中添加和访问控制器
如果我有一个控制器,如何使用新添加的方法通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的路由表应按此顺序排列,
结束。
以作为最常见的情况
Your routing table should have it in this order
to end with
as your most general case.
只需为第二种情况指定正确的路由模式,并确保记住映射是从上到下评估的(第一个匹配被执行)。
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).