ruby on rails - CommentsController中的ActiveModel :: ForbiddenAttributesError #create

use*_*901 0 ruby ruby-on-rails ruby-on-rails-4

ActiveModel::ForbiddenAttributesError
Extracted source (around line #3):

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.create!(params[:comment])
  redirect_to @post
end

Rails.root: C:/Users/ManU/Desktop/quick_blog  
Application Trace | Framework Trace | Full Trace

app/controllers/comments_controller.rb:4:in `create'
Run Code Online (Sandbox Code Playgroud)

我应该做些什么来处理这个错误.....

小智 5

我有同样的错误,由于某种原因,他们删除了评论创建行上的.permit部分.如果你使用原件:

@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
Run Code Online (Sandbox Code Playgroud)

而不是新的:

@comment = @post.comments.create(params[:comment])
Run Code Online (Sandbox Code Playgroud)

它工作正常.所以该文件最终看起来像:

class CommentsController < ApplicationController

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

  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end

end
Run Code Online (Sandbox Code Playgroud)