'ActionView::Template::Error - 没有路由匹配'超越“旧风格” “link_to”的参数陈述

发布于 2024-11-27 04:09:06 字数 1153 浏览 1 评论 0原文

我正在使用 Ruby on Rails 3.0.9,遇到了这个问题: 为“link_to”语句设置“:controller”参数时出现问题。没有人回答解决方案,但是,在尝试并重新尝试查找并解决问题时,我发现在传递“旧式”参数时(即使用 controlleraction< /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 技术交流群。

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

发布评论

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

评论(1

护你周全 2024-12-04 04:09:06

“无路由错误”一般发生在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.

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