Rails ActiveModel :: ForbiddenAttributesError

时间:2013-12-24 19:24:59

标签: ruby-on-rails ruby-on-rails-4

rarils 4.0.0 我正在尝试发表评论,但我有一个错误:

CommentController中的ActiveModel :: ForbiddenAttributesError #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"=>"g12345@12345.com",
 "body"=>"hello hello"},
 "commit"=>"Add",
 "article_id"=>"5"}

我的评论/ 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 个答案:

答案 0 :(得分:1)

Rails 4默认使用强参数。你有类似的东西:

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

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

CommentsController

它看起来像这样:

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