2013-09-08 16 views
13

私は少し失われています。私はRails 4のエラーを取得しています:ActiveModel :: ForbiddenAttributesError。これは、私がやったアイテムを渡すことを許可する必要があることを意味しますが、私は何かを欠いているに違いありません。ActiveModel :: ForbiddenAttributesError - Rails 4

コメントコントローラー:

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 

コメントの移行

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 

を作成します。私はここで何をしないのですか?あなたが他のコードを見る必要があるかどうかを教えてください。

ありがとうございます!

答えて

32

代わりの

@comment = @post.comments.create!(params[:comment]) 

あなたはあなたが許可された属性を使用せずに、すべてのハードワークをした

@comment = @post.comments.create!(comment_params) 

をしたいです!

+0

Blah!私はこれが自明でなければならないことを知っていた。ありがとう! –

+1

私もレール3から来てくれました。ありがとう! – Zach