开发者

Rails: how are paths generated?

开发者 https://www.devze.com 2023-01-11 20:13 出处:网络
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.

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 


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| %>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消