1

rarils 4.0.0 I'm trying to post a comments but I have an error:

ActiveModel::ForbiddenAttributesError in CommentsController#create ActiveModel::ForbiddenAttributesError

def create
@comment = @article.comments.new(params[:comment]) #error point highlight this line

Parameters
{"utf8"=>"✓",
 "authenticity_token"=>"zSq3KpEbucFQLa6XStEJ/I0+CpKPLFYcU/WGIdneeMg=",
 "comment"=>{"name"=>"g12345",
 "email"=>"[email protected]",
 "body"=>"hello hello"},
 "commit"=>"Add",
 "article_id"=>"5"}

my comments/new.html.erb

<%= form_for([@article, @article.comments.new], remote: true) do |f| %>
<%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit 'Add' %>
</div>
<% end %>
1
  • 1
    Please post your comments_controller code. Commented Dec 24, 2013 at 19:26

1 Answer 1

1

Rails 4 uses strong parameters by default. Do you have something like:

params.require(:some_param).permit(...)

or

params.permit(:list, :of, :allowed, :params)

in your CommentsController?

It would look something like this:

class CommentsController < ApplicationController

  def create
    @comment = @article.comments.new(comment_params) #error point highlight this line
  end

  private

  def comment_params
    params.require(:comment).permit(:name, :email, :body)
  end

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

2 Comments

Thks CDub I don't have something like that in my Comments Controller. I only have as a private function : code def load_article @article = Article.find(params[:article_id]) end
That's causing your issue. See my example above, and eliminate whichever parameters you don't want to permit to mass assignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.