首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails ActiveModel::ForbiddenAttributesError

Rails ActiveModel::ForbiddenAttributesError
EN

Stack Overflow用户
提问于 2013-12-24 19:24:58
回答 1查看 268关注 0票数 1

Rarils4.0.0我试图发布一个评论,但我有一个错误:

ActiveModel::CommentsController#create中的ForbiddenAttributesError

代码语言:javascript
复制
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

代码语言:javascript
复制
<%= 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 %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-24 19:26:52

Rails 4默认使用强参数。你有这样的东西吗?

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

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

在你的CommentsController

看起来会是这样的:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20765861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档