简体   繁体   中英

ActiveModel::ForbiddenAttributesError - Rails 4

I'm a bit lost. I an getting the Rails 4 error: ActiveModel::ForbiddenAttributesError. I understand that this means I need to permit items to pass, which I have done, but I must be missing something.

Comments Controller:

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

Create Comments Migration

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

What am I missing here? Let me know if you need to see any other code.

Thanks!

Instead of

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

you want

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

You did all the hard work without using the permitted attributes!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM