Ruby on rails Rails map.resources&;帮助器URL
我有两个这样的模型:Ruby on rails Rails map.resources&;帮助器URL,ruby-on-rails,url-routing,Ruby On Rails,Url Routing,我有两个这样的模型: class Topic < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :user belongs_to :topic has_many :comments end <% form_for(@article) do |f| %> ... <% end %> 当我调用适当的URL(例如
class Topic < ActiveRecord::Base
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :topic
has_many :comments
end
<% form_for(@article) do |f| %>
...
<% end %>
当我调用适当的URL(例如,/:topic\u slug/articles/2
)时,我可以很好地查看文章。在我的文章中,我使用partial来处理文章的创建和编辑,如下所示:
class Topic < ActiveRecord::Base
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :topic
has_many :comments
end
<% form_for(@article) do |f| %>
...
<% end %>
...
当我尝试创建新文章或编辑现有文章时,出现以下错误:
NoMethodError in Articles#new
Showing app/views/articles/_form.html.erb where line #1 raised:
undefined method `articles_path' for #<ActionView::Base:0x103d11dd0>
Extracted source (around line #1):
1: <% form_for(@article) do |f| %>
2: <%= f.error_messages %>
3:
4: <p>
Trace of template inclusion: app/views/articles/new.html.erb
文章中的命名错误#新增
显示第1行出现的app/views/articles/_form.html.erb:
未定义的方法“articles\u path”#
提取的源(第1行附近):
1:
2:
三:
4:
模板包含跟踪:app/views/articles/new.html.erb
有人知道我哪里出了问题,我遗漏了什么吗?您还需要通过以下主题:
<% form_for([@topic, @article]) do |f| %>
然后:
<% form_for(@article) do |f| %>
将正常工作,但它将生成url,如:
/articles/3
-没有主题部分。您还需要传递主题:
<% form_for([@topic, @article]) do |f| %>
然后:
<% form_for(@article) do |f| %>
将正常工作,但它将生成url,如:
/articles/3
-无主题部分。如果您想要文章的非嵌套路由,请分别映射文章:
map.resources :articles
如果要为项目创建非嵌套路由,请分别映射项目:
map.resources :articles