Rails 5具有强参数的ActiveRecord :: ForbiddenAttributesError

时间:2018-02-05 18:36:33

标签: ruby-on-rails ruby activerecord attributes

我正在尝试将嵌套的评论添加到我的论坛项目中。这些评论与2种不同类型的论坛帖子(艺术品和邮政)有多态关联。我正在关注本教程: http://www.tweetegy.com/2013/04/create-nested-comments-in-rails-using-ancestry-gem/

目前,当我汇总我的评论表单时,我收到此错误:

帖子中的'ActiveModel :: ForbiddenAttributesError :: CommentsController #create'

有问题的行在我的评论控制器的create方法中:

def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment]) # This is the line that causes the error
    # @comment = @commentable.comments.build(comment_params)--when changed to this, the submit button simply stops submitting anything
    @comment.user = current_user
    if @comment.save
        flash[:notice] = "Successfully created comment"
            redirect_to @commentable
    else
        flash[:error] = "Error adding comment"
    end
end

其余的comments_controller:

class CommentsController < ApplicationController
before_action :authenticate_user!

def new
    @parent_id = params.delete(:parent_id)
    @commentable = find_commentable
    @comment = Comment.new(:parent_id => @parent_id, :commentable_id => @commentable.id, :commentable_type => @commentable.class.to_s)
end

def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment]) # This is the line that causes the error
    # @comment = @commentable.comments.build(comment_params)--when changed to this, the submit button simply stops submitting anything
    @comment.user = current_user
    if @comment.save
        flash[:notice] = "Successfully created comment"
            redirect_to @commentable
    else
        flash[:error] = "Error adding comment"
    end
end

def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy
    redirect_to @commentable, notice: "Your Comment was successfully deleted"
end

def upvote
    @comment = Comment.find(params[:id])
    @comment.upvote_by current_user
    redirect_back(fallback_location: root_path)
end

def downvote
    @comment = Comment.find(params[:id])
    @comment.downvote_by current_user
    redirect_back(fallback_location: root_path) #maybe change to - :back returns you to the same page - upvotes/downvotes can be on index and show pages
end

private

def comment_params
  params.require(:comment).permit(:parent_id, :comment)
end 

def find_commentable
  params.each do |name, value|
      if name =~ /(.+)_id$/
          return $1.classify.constantize.find(value)
      end
  end
  nil
end
end

评论表:

= form_for [@commentable, @comment] do |f|
= f.hidden_field :parent_id
= f.text_area :comment
= f.submit

评论模型:

class Comment < ApplicationRecord
  acts_as_votable
  has_ancestry
  attr_accessor :content, :parent_id, :commentable_id, :commentable_type
  belongs_to :user
  belongs_to :commentable, polymorphic: true
end

我对红宝石很陌生,所以我不确定我做错了什么......这让我困扰了很多年,因为我无法分辨哪些属性被传递和禁止。

非常感谢任何帮助。

编辑:这是用于创建评论的开发日志

在2018-02-06 14:35:21 +0000开始发布“/ posts / 5 / comments”for 127.0.0.1 Posts处理:: CommentsController #create as HTML   参数:{ “UTF8”=&gt; “中A”, “authenticity_token”=&gt; “中NQ25DUzZ0di2L + rdARO8jEmkb / flm1hEAO7tLj331CbTwJUPGarzBTcTR + D0oV1xExwLVZ7bjrheVm1sGqh7dg ==”, “注释”=&GT; { “PARENT_ID”=&gt; “中”, “评论” =&gt;“评论测试”},“提交”=&gt;“创建评论”,“post_id”=&gt;“5”}   [1m [36mUser Load(0.3ms)[0m [1m [34mSELECT“users”。* FROM“users”WHERE“users”。“id”=? ORDER BY“users”。“id”ASC LIMIT?[0m [[“id”,2],[“LIMIT”,1]]   [1m [36mPost Load(0.1ms)[0m [1m [34mSELECT“post”。* FROM“posts”WHERE“posts”。“id”=? LIMIT?[0m [[“id”,5],[“LIMIT”,1]]   [1m [36mCACHE后加载(0.0ms)[0m [1m [34mSELECT“发布”。* FROM“发布”WHERE“发布”。“id”=? LIMIT?[0m [[“id”,5],[“LIMIT”,1]] 在4ms内完成401 Unauthorized(ActiveRecord:0.5ms)

ActiveModel :: ForbiddenAttributesError(ActiveModel :: ForbiddenAttributesError):

app / controllers / comments_controller.rb:12:在'create'

0 个答案:

没有答案