我有一个rails博客,其中所有帖子都嵌套在类别下,现在我添加的评论嵌套在帖子下但表单却抛出undefined method `post_comments_path'
错误。
我想我需要让@posts像@ categories.post一样,但我不确定。
resources :categories do
resources :posts, path: 'article' do
resources :comments, :only => [:create]
end
end
def create
@post = Post.find(params[:post_id])
@comment = @posts.comments.create!(params[:comment])
redirect_to @post
end
<%= simple_form_for [@post, Comment.new ], :remote => true do |f| %>
<%= f.input :name %>
<%= f.input :email %>
<%= f.input :comment %>
<%= f.button :submit %>
<% end %>
答案 0 :(得分:1)
我认为你忘记了类别。您需要提供一个类别:
def new
...
@category = Category.find(params[:category_id])
...
end
def create
@post = Post.find(params[:post_id])
@comment = @posts.comments.create!(params[:comment])
redirect_to @post
end
<%= simple_form_for [@category, @post, Comment.new ], :url => category_post_comments_path, :remote => true do |f| %>
<%= f.input :name %>
<%= f.input :email %>
<%= f.input :comment %>
<%= f.button :submit %>
<% end %>
或从路线中删除类别:
resources :posts, path: 'article' do
resources :comments, :only => [:create]
end