繁体   English   中英

Rails ActiveModel :: ForbiddenAttributesError

[英]Rails ActiveModel::ForbiddenAttributesError

rarils 4.0.0,我正在尝试发表评论,但出现错误:

CommentsController#create ActiveModel :: ForbiddenAttributesError中的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 %>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM