ActiveModel::ForbiddenAttributesError in BlogPostsController#comments
So today while going through the legendary “Create a Blog with Rails” tutorial I got this fantastic error message:
Okay, so some or other attributes are forbidden well thanks that is informative. So this helped a bit though at least I knew what line was giving me an error.
1 2 3 # POST /comment def comment @comment = Comment.new(params[:comment]) #Error is here
So I obviously googled this and got to this StackOverflow answer
I guess you are using Rails 4. If so, the needed parameters must be marked as required.
1 2 3 4 def user_params params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password) end
So all I needed was to add this
1 2 3 def comment_params params.require(:comment).permit(:email, :comment, :blog_post_id) end
and change my comment method to
1 2 3 # POST /comment def comment @comment = Comment.new(comment_params)
I struggled with this for a while and looking back I feel like an idiot that the fix was so easy o_O.