Rails:路径是如何生成的?
在 Rails 3 中,当为“类别”生成一个脚手架时,将在 erb 视图中使用categories_path(以及 edit_category_path(@category), ...)。
这不是一个随处可见的变量,并且可能是生成的。然而,就我而言,对于不同的实体 Article,我首先生成模型,然后生成控制器。现在,当我尝试输出articles_path时,我得到了
#<#:0x000001019d1be0> 的未定义方法“articles_path”
我什至不能使用 <%= form_for(@article) do |f| %>
因为这会产生相同的错误。
我该怎么办?
我的路由是这样的:
resources :categories do
resources :articles
end
In Rails 3, when a scaffold is generated for instance for a 'Category' the will be a categories_path (and a edit_category_path(@category), ...) used in the erb views.
This is not a variable that can be found anywhere and probably is generated. However in my case, for a different entity, Article, I first generated the model and then the controller. Now when I try output an articles_path, I get a
undefined method `articles_path' for #<#:0x000001019d1be0>
I cannot even use a <%= form_for(@article) do |f| %>
as this generates the same error.
What am I supposed to do?
My routings are like this:
resources :categories do
resources :articles
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文章资源位于类别范围内,因此正确的使用路径是
category_articles_path(@category)
或edit_category_articles_path(@category, @article)
。要为您的 form_for 执行此操作,请尝试:The articles resource is within the categories scope, so the correct path to use would be
category_articles_path(@category)
oredit_category_articles_path(@category, @article)
. To do this for your form_for, try:由于文章位于类别范围内,因此您需要使用category_articles_path。
As article lives in the category scope, you need to use category_articles_path.