0
ActiveModel::ForbiddenAttributesError
Extracted source (around line #3):

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

Rails.root: C:/Users/ManU/Desktop/quick_blog  
Application Trace | Framework Trace | Full Trace

app/controllers/comments_controller.rb:4:in `create'

What i'm supposed to do to deal with this error.....

3
  • Can you again confirm if you using Rails3 or 4? You have tagged this as rails3. This error is very common for newbies using rails 4 Commented Nov 8, 2013 at 13:37
  • 1
    I'd say you're supposed to google first, and/or read the docs, because that topic is widely discussed. Commented Nov 8, 2013 at 13:40
  • yes, i'm using rails4..??? what i'm supposed to do now... Commented Nov 8, 2013 at 13:44

3 Answers 3

5

I had the same error, for some reason they delete the .permit part on the comment creation line. If you use the original:

@comment = @post.comments.create(params[:comment].permit(:commenter, :body))

Rather than the new:

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

It works fine. So that file ends up looking like:

class CommentsController < ApplicationController

  def create
    @post = Post.find(params[:post_id])
    #@comment = @post.comments.create(params[:comment])
    @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
    redirect_to post_path(@post)
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end

end
Sign up to request clarification or add additional context in comments.

Comments

1

The ForbiddenAttributesError is related with Strong Parameters

Either you installed the gem in your Rails3 app or you miss tagged the question and you're using Rails4, where the gem comes by default.

Either way, with Strong Parameters the parameter checking leaves the Model and passes to the controller.

Where before you would had something like attr_accessible :foo, :bar in the model, now you need to have something like

def comment_params
  params.permit(:foo, :bar)
end

in the controller, and then call Comment.create!(comment_params)

2 Comments

please specify path as well..where i have to edit...this is my application location C:\Users\ManU\Desktop\quick_blog
For what I told you, you should look in the Comment model and controller which should be in quick_blog/app/models/comment.rb and quick_blog/app/controllers/comments_controller.rb
1

Hope this works! (I got the same error and the following change worked for me)

def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.