【问题标题】:Mass assignment error using acts_as_commentable_with_threading使用acts_as_commentable_with_threading 的质量分配错误
【发布时间】:2013-06-08 08:46:20
【问题描述】:

我一直在关注 this tutorial 以使用 acts_as_commentable_with_threading gem 实现线程化 cmets。但是,我似乎遇到了质量分配错误,这似乎源于 gem 设置 Comment 模型的方式,我不确定是否应该修改(出于图书馆的初衷)。

错误输出

Can't mass-assign protected attributes: commentable, body, user_id
app/models/comment.rb:20:in `new'
app/models/comment.rb:20:in `build_from'
app/controllers/posts_controller.rb:21:in `show'

posts_controller.rb

def show
  @post = Post.find(params[:id])
  @comments = @post.comment_threads.order('created_at desc')
  @new_comment = Comment.build_from(@post, current_user.id, "")
end

cmets_controller.rb

class CommentsController < ApplicationController
  def create
    @comment_hash = params[:comment]
    @obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
    @comment = Comment.build_from(@obj, current_user.id, @comment_hash[:body])
    if @comment.save
      render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
    else
      flash.now[:error] = 'Comment was not submitted.'
    end
  end
end

comment.rb

class Comment < ActiveRecord::Base
  acts_as_nested_set :scope => [:commentable_id, :commentable_type]

  validates :body, :presence => true
  validates :user, :presence => true

  # NOTE: install the acts_as_votable plugin if you
  # want user to vote on the quality of comments.
  #acts_as_votable

  belongs_to :commentable, :polymorphic => true

  # NOTE: Comments belong to a user
  belongs_to :user

  # Helper class method that allows you to build a comment
  # by passing a commentable object, a user_id, and comment text
  # example in readme
  def self.build_from(obj, user_id, comment)
    new \
      :commentable => obj,
      :body        => comment,
      :user_id     => user_id
  end

  #helper method to check if a comment has children
  def has_children?
    self.children.any?
  end

  # Helper class method to lookup all comments assigned
  # to all commentable types for a given user.
  scope :find_comments_by_user, lambda { |user|
    where(:user_id => user.id).order('created_at DESC')
  }

  # Helper class method to look up all comments for
  # commentable class name and commentable id.
  scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
    where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
  }

  # Helper class method to look up a commentable object
  # given the commentable class name and id
  def self.find_commentable(commentable_str, commentable_id)
    commentable_str.constantize.find(commentable_id)
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mass-assignment acts-as-commentable


    【解决方案1】:

    尝试添加Comment模型

    attr_accessible :commentable, :body, :user_id
    

    UPD:更多关于批量分配保护here

    UPD2。但我建议不要通过批量分配分配user_id。最好在你的控制器中使用current_user.comments.build_from

    【讨论】:

    • 这是否可取?我不希望 user_id 被大量分配,因为它应该被限制为 current_user,对吧? (此外,gem 的文档中没有任何内容指定以任何方式修改模型以供使用。)
    【解决方案2】:

    尝试像这样修改 build_from 方法:

    def self.build_from(obj, user_id, comment)
        new do |c|
          c.commentable = obj
          c.body        = comment
          c.user_id     = user_id
        end
    end
    

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多