Rails:路径是如何生成的?

发布于 2024-09-15 12:31:25 字数 485 浏览 7 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(2

晒暮凉 2024-09-22 12:31:25

文章资源位于类别范围内,因此正确的使用路径是 category_articles_path(@category)edit_category_articles_path(@category, @article)。要为您的 form_for 执行此操作,请尝试:

<%= form_for([@category, @article]) do |f| %>

The articles resource is within the categories scope, so the correct path to use would be category_articles_path(@category) or edit_category_articles_path(@category, @article). To do this for your form_for, try:

<%= form_for([@category, @article]) do |f| %>
毁虫ゝ 2024-09-22 12:31:25

由于文章位于类别范围内,因此您需要使用category_articles_path

As article lives in the category scope, you need to use category_articles_path.

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