“没有路线匹配”在 Rails 3 中使用 current_page 时
有没有人在使用 current_page 时经历过路由神秘地变得不可检测?在 Rails 3 中?即使使用完全生成的带有路由、视图和控制器的脚手架,我也会收到“没有路由匹配”错误。
代码如下:
if current_page?(:controller => 'users', :action => "show")
如果我向routes.rb添加“match”命令,它可以正常工作,但如果资源已经创建,为什么我需要这样做呢?我缺少什么?
Has anybody experienced routes mysteriously becoming undetectable when using current_page? in Rails 3? Even with a fully generated scaffold complete with routes, a view, and a controller, I am getting a "No route matches" error.
Here's the code:
if current_page?(:controller => 'users', :action => "show")
If I add a "match" command to routes.rb, it works fine, but why would I need to do that if the resources have already been created? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想测试当前控制器,可以执行以下操作:
同样,如果您使用命名空间控制器,则可以仅使用斜杠将命名空间与控制器名称分开,例如:
If you just want to test the current controller, you can do the following:
Similarly, if you're using a namespaced controller, you can just use a slash to separate the namespace(s) from the controller name, e.g.:
您缺少此帮助程序中的
id
参数:它希望您传递完整的路线。如果您不想要这个,只想在控制器和操作上进行匹配,那么我建议您自己编写代码。
You're missing the
id
parameter from this helper:It expects you to pass a full route through. If you don't want this and only want to match on the controller and action then I would recommend coding your own.
根据您的路线,要查找没有 ID 的通用
show
操作,您可以查找例如!current_page?(:controller => "users", :action => "index" )
。Depending on your routes, to look for a generic
show
action without ID you could look for e.g.!current_page?(:controller => "users", :action => "index")
.