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

ActiveModel::ForbiddenAttributesError 4.1
EN

Stack Overflow用户
提问于 2014-07-22 09:13:54
回答 1查看 55关注 0票数 0

在我的应用程序上创建注释时,我得到了下面的。

错误消息,确切地说问题来自我的Comments Controller文件中的第7行:@comment = @pin.comments.create(params[:comment])

app/控制器/注释_Controller.rb

代码语言:javascript
复制
class CommentsController < ApplicationController

  before_filter :authenticate_user!

  def create
    @pin = Pin.find(params[:pin_id])
    @comment = @pin.comments.create(params[:comment])

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

  end

  private
  def comment_params
      params.require(:comment).permit(:body, :pin_id)
    end


end

以下是评论模型

代码语言:javascript
复制
class Comment < ActiveRecord::Base

      belongs_to :pin

end

对此错误消息有帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-22 09:15:08

您应该将这一行替换为

代码语言:javascript
复制
@comment = @pin.comments.create(comment_params)

此外,将pin_id放入允许的参数中是不必要的(因为您通过@pin.comments关联创建注释),而且可能不安全(用户可以将注释与其他Pin相关联)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24883321

复制
相关文章

相似问题

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