未定义的方法许可

时间:2014-02-13 14:16:10

标签: ruby-on-rails-3

我正在练习rails guide中的帖子。在评论控制器中,我这样写,但它出现了错误

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
    redirect_to post_path(@post)
  end
end

1 个答案:

答案 0 :(得分:1)

我建议您跟进此guide

这应该有效:

class CommentsController < ApplicationController
  def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create(comment_params)
    redirect_to article_path(@article)
  end

  private
    def comment_params
      params.require(:comment).permit(:commenter, :body)
    end
end