Reputation:
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!
Upvotes: 13
Views: 7913
Reputation: 18037
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!
Upvotes: 32