获取未定义的局部变量或方法以进行投票acts_as_votable Rails

时间:2014-08-02 07:14:20

标签: ruby-on-rails ruby voting

我使用Commontator gem启用了用户的评论。已经设置了Commontator gem以轻松集成acts_as_votable。我按照说明启用Voting

config/initializers/commontator.rb/我改变了config.comment_voting = :n(没有投票) 至 config.comment_voting = :ld(喜欢/不喜欢)。

现在,当我尝试创建评论时,我收到错误:

NameError in Pin#show

undefined local variable or method votes'

-

<% if comment.can_be_voted_on? %>
     <% can_vote = comment.can_be_voted_on_by?(user) %>
     <% vote = comment.get_vote_by(user) %>
     <% comment_voting = comment.thread.config.comment_voting.to_sym %>
     <% if comment_voting == :ld || comment_voting == :l %>

这是我的评论模型

module Commontator
  class Comment < ActiveRecord::Base
    belongs_to :creator, :polymorphic => true
    belongs_to :editor, :polymorphic => true
    belongs_to :thread

    has_one :commontable, :through => :thread

    validates_presence_of :creator, :on => :create
    validates_presence_of :editor, :on => :update
    validates_presence_of :thread
    validates_presence_of :body

    validates_uniqueness_of :body,
      :scope => [:creator_type, :creator_id, :thread_id],
      :message => I18n.t('commontator.comment.errors.double_posted')

    protected

    cattr_accessor :acts_as_votable_initialized

    public

    def is_modified?
      !editor.nil?
    end

    def is_latest?
      thread.comments.last == self
    end

    def is_votable?
      return true if acts_as_votable_initialized
      return false unless self.class.respond_to?(:acts_as_votable)
      self.class.acts_as_votable
      self.class.acts_as_votable_initialized = true
    end

    def get_vote_by(user)
      return nil unless is_votable? && user && user.is_commontator
      votes.where(:voter_type => user.class.name, :voter_id => user.id).first
    end

    def update_cached_votes(vote_scope = nil)
      self.update_column(:cached_votes_up, count_votes_up(true))
      self.update_column(:cached_votes_down, count_votes_down(true))
    end

    def is_deleted?
      !deleted_at.blank?
    end

    def delete_by(user)
      return false if is_deleted?
      self.deleted_at = Time.now
      self.editor = user
      self.save
    end

    def undelete_by(user)
      return false unless is_deleted?
      self.deleted_at = nil
      self.editor = user
      self.save
    end

    def created_timestamp
      I18n.t 'commontator.comment.status.created_at',
             :created_at => I18n.l(created_at, :format => :commontator)
    end

    def updated_timestamp
      I18n.t 'commontator.comment.status.updated_at',
             :editor_name => Commontator.commontator_name(editor || creator),
             :updated_at => I18n.l(updated_at, :format => :commontator)
    end

    ##################
    # Access Control #
    ##################

    def can_be_created_by?(user)
      user == creator && user.is_commontator &&\
      !thread.is_closed? && thread.can_be_read_by?(user)
    end

    def can_be_edited_by?(user)
      return true if thread.can_be_edited_by?(user) &&\
                     thread.config.moderator_permissions.to_sym == :e
      comment_edit = thread.config.comment_editing.to_sym
      !thread.is_closed? && !is_deleted? && user == creator &&\
      comment_edit != :n && (is_latest? || comment_edit == :a) &&\
      thread.can_be_read_by?(user)
    end

    def can_be_deleted_by?(user)
      mod_perm = thread.config.moderator_permissions.to_sym
      return true if thread.can_be_edited_by?(user) &&\
                     (mod_perm == :e ||\
                       mod_perm == :d)
      comment_del = thread.config.comment_deletion.to_sym
      !thread.is_closed? && (!is_deleted? || editor == user) &&\
      user == creator && comment_del != :n &&\
      (is_latest? || comment_del == :a) &&\
      thread.can_be_read_by?(user)
    end

    def can_be_voted_on?
      !thread.is_closed? && !is_deleted? &&\
      thread.config.comment_voting.to_sym != :n && is_votable?
    end

    def can_be_voted_on_by?(user)
      !user.nil? && user.is_commontator && user != creator &&\
      thread.can_be_read_by?(user) && can_be_voted_on?
    end
  end
end

更新

votes.where(:voter_type => user.class.name, :voter_id => user.id).first更改为 votes_for.where(:voter_type => user.class.name, :voter_id => user.id).first

我现在得到一种不同的错误: ActionView::Template::Error (Could not find table 'votes'):

2 个答案:

答案 0 :(得分:3)

尝试重新启动服务器。我这样解决了。

答案 1 :(得分:1)

替换此行:

votes.where(:voter_type => user.class.name, :voter_id => user.id).first

votes_for.where(:voter_type => user.class.name, :voter_id => user.id).first