【问题标题】:ActiveModel::ForbiddenAttributesError using an older version of ruby? [duplicate]ActiveModel::ForbiddenAttributesError 使用旧版本的 ruby​​? [复制]
【发布时间】:2014-11-24 05:18:09
【问题描述】:

我正在使用 ruby​​ on rails 创建一个博客。我正在关注在 Rails 上使用旧版本的教程。我正在尝试在同一页面上发布有关博客文章的 cmets。每当我点击提交按钮时,我都会收到此错误消息。 ActiveModel::ForbiddenAttributesError

提取的源代码(第 44 行附近):

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

respond_to do |format|
  if @comment.save

这是我在控制器中的代码

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

  respond_to do |format|
    if @comment.save
      format.html { redirect_to @post, notice: 'Comment was successfully created.' }
      format.json { render json: @post, status: :created, location: @comment }
    else
      format.html { render action: "new" }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end

感谢您的帮助

【问题讨论】:

  • 你能显示comment.rb文件的行,上面写着:attr_asseccible,同时发布表单代码。

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


【解决方案1】:

我猜你使用的是 Rails 4。如果是这样,需要的参数必须在控制器中标记为必需。

您可以参考http://stackoverflow.com/questions/17868427/rails-4-activemodelforbiddenattributeserror

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多