'ActionView::Template::Error - 没有路由匹配'超越“旧风格” “link_to”的参数陈述
我正在使用 Ruby on Rails 3.0.9,遇到了这个问题: 为“link_to”语句设置“:controller”参数时出现问题。没有人回答解决方案,但是,在尝试并重新尝试查找并解决问题时,我发现在传递“旧式”参数时(即使用 controller
和 action< /code> 选项而不是命名路由路径)有时
link_to
无法按预期工作(即,您会收到 ActionView::Template::Error - No Route matches
错误,尽管如果您有正确说明的控制器和路线)。无论如何,你可以像下面这样设置:
<%= link_to("New article", {:controller => '../', :action => 'new'}) %> # Note the "'../'"
它可以工作。
我想知道何时和为什么,一般来说,它会发生:controller
选项似乎“被迫”引用相对或绝对路径而不是您传入的控制器参数。
也就是说,如果我设置 :controller =>; articles
,我在由 controllers/article/categories_controller.rb
管理的视图文件中使用上述代码,我收到以下错误:
`ActionView::Template::Error (No route matches {:controller=>"articles/categories/articles", :action=>"new"})`
因为,如上所述,控制器似乎引用/articles/categories
路径而不是 /articles
路径(因为在本例中,我将控制器设置为 articles
) 。会不会是路由器的问题?
I am using Ruby on Rails 3.0.9 and I had this problem: Trouble on setting the ':controller' parameter for a 'link_to' statement. No one answered with a solution but, trying and re-trying to find and solve the problem, I have found that on passing "old style" parameters (that is, using controller
and action
options instead of named route paths) sometime a link_to
doesn't work as expected (that is, you get a ActionView::Template::Error - No route matches
error although if you have controller and routes properly stated). Anyway you can set for that something like the following:
<%= link_to("New article", {:controller => '../', :action => 'new'}) %> # Note the "'../'"
and it works.
I would like to know when and why, in general, it happens: the controller
option seems "forced" to refer to the relative or absolute path and not to the controller parameter you passed in.
That is, if I set :controller => articles
and I use the above code in a view file managed by the controllers/article/categories_controller.rb
I get the following error:
`ActionView::Template::Error (No route matches {:controller=>"articles/categories/articles", :action=>"new"})`
because, as said above, the controller seems to refer to the /articles/categories
path instead of /articles
path (as it should be since, in this example, I set the controller to articles
). Can be it a router problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“无路由错误”一般发生在routes.rb文件中没有提到正确的路由时。它与绝对/相对路径无关。
The "no route error " generally occurs when there is no proper route mentioned in routes.rb file.It has nothing to do with the absolute/relative path.