ActiveModel :: ForbiddenAttributesError - Rails 4

13 ruby-on-rails ruby-on-rails-4

我有点迷茫.我得到了Rails 4错误:ActiveModel :: ForbiddenAttributesError.我明白这意味着我需要允许物品通过,我已经完成了,但我必须遗漏一些东西.

评论控制器:

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

    private
        # Never trust parameters from the scary internet, only allow the white list through.
        def comment_params
            params.require(:comment).permit(:post_id, :comment, :body)
        end

end
Run Code Online (Sandbox Code Playgroud)

创建评论迁移

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.references :post
      t.text :body

      t.timestamps
    end
  end

  def self.down
    drop_table :comments
  end  
end
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?如果您需要查看任何其他代码,请告诉我们.

谢谢!

pdo*_*obb 32

代替

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

你要

@comment = @post.comments.create!(comment_params)
Run Code Online (Sandbox Code Playgroud)

你没有使用允许的属性就完成了所有艰苦的工作!