Ruby on rails 获取投票行为的未定义局部变量或方法作为可投票轨道
我正在使用启用来自用户的评论。Commontator gem已设置为易于集成。我按照指示启用 在Ruby on rails 获取投票行为的未定义局部变量或方法作为可投票轨道,ruby-on-rails,ruby,voting,Ruby On Rails,Ruby,Voting,我正在使用启用来自用户的评论。Commontator gem已设置为易于集成。我按照指示启用 在config/initializers/commontator.rb/中,我更改了config.comment\u投票=:n(无投票) 到 config.comment\u投票=:ld(喜欢/不喜欢) 现在,当我尝试创建注释时,出现了一个错误: Pin中的名称错误#显示 未定义的局部变量或方法投票' -- 这是我的评论模型 module Commontator class Comment &l
config/initializers/commontator.rb/
中,我更改了config.comment\u投票=:n
(无投票)
到
config.comment\u投票=:ld
(喜欢/不喜欢)
现在,当我尝试创建注释时,出现了一个错误:
Pin中的名称错误#显示
未定义的局部变量或方法投票'
--
这是我的评论模型
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
modulecommontator
类注释true
属于:编辑器,:多态=>true
属于:线程
有一个:commontable,:through=>:线程
验证是否存在:creator,:on=>:create
验证是否存在:编辑器,:on=>:更新
验证线程是否存在
验证是否存在:正文
验证:主体的唯一性,
:scope=>[:创建者类型,:创建者id,:线程id],
:message=>I18n.t('commontator.comment.errors.double_posted')
受保护的
cattr\u访问器:作为\u votable\u初始化
平民的
def是否已修改?
!编辑:零?
终止
def是最新的吗?
thread.comments.last==self
终止
def是否可以使用?
如果操作为初始化的VOU表格,则返回true
返回false,除非self.class.response\u to?(:充当\u votable)
self.class.acts_作为_votable
self.class.act_as_votable_initialized=true
终止
def获取投票人(用户)
返回零,除非可选择user&&user.is\u commontator
其中(:voter\u type=>user.class.name,:voter\u id=>user.id)
终止
def更新缓存的投票(投票范围=nil)
self.update列(:缓存的投票数向上,计数投票数向上(true))
self.update列(:缓存的投票数减少,计数投票数减少(true))
终止
def是否已删除?
!在空白处删除?
终止
def delete_by(用户)
如果被删除,返回false?
self.deleted_at=Time.now
self.editor=用户
自救
终止
def undelete_by(用户)
除非被删除,否则返回false?
self.deleted_at=nil
self.editor=用户
自救
终止
def创建的时间戳
I18n.t'commontator.comment.status.created_at',
:created_at=>I18n.l(created_at,:format=>:commontator)
终止
def更新的时间戳
I18n.t'commontator.comment.status.updated_位于',
:editor_name=>Commontator.Commontator_name(editor | | creator),
:updated_at=>I18n.l(updated_at,:format=>:commontator)
终止
##################
#访问控制#
##################
def可以由?(用户)创建吗
user==creator&&user.is\u commontator&&\
!thread.u是否已关闭?&&线程。可以被?(用户)读取吗
终止
def可以由?(用户)编辑吗
如果是线程,则返回true。是否可以由?(用户)编辑&&\
thread.config.moderator_permissions.to_sym==:e
comment\u edit=thread.config.comment\u editing.to\u sym
!thread.u是否已关闭?&&!_是否已删除?&&用户==创建者&&\
注释\u编辑!=:n&(是最新的吗?|注释|编辑==:a)&&\
线程。可以被?(用户)读取吗
终止
def可以由?(用户)删除吗
mod_perm=thread.config.moderator\u permissions.to\u sym
如果是线程,则返回true。是否可以由?(用户)编辑&&\
(mod_perm==:e||\
mod_perm==:d)
comment\u del=thread.config.comment\u deletation.to\u sym
!thread.u是否已关闭?&&(!是否已删除?| |编辑器==用户)&&\
用户==创建者和注释\u del!=:n&&\
(是最新的吗?|评论| del==:a)&&\
线程。可以被?(用户)读取吗
终止
def可以投票表决吗?
!thread.u是否已关闭?&&!你被删除了吗&&\
thread.config.comment_voting.to_sym!=:n&&U可供选择吗?
终止
def可以由?(用户)投票表决吗
!user.nil?&&user.is_commontator&&user!=创造者&&\
线程。可以被?(用户)读取吗?&可以投票吗?
终止
终止
终止
更新
更改投票。其中(:voter\u type=>user.class.name,:voter\u id=>user.id)。首先将更改为
投票给.where(:voter\u type=>user.class.name,:voter\u id=>user.id)。首先
我现在遇到了另一种错误:
ActionView::Template::Error(找不到表“投票”):
替换此行:
votes.where(:voter_type => user.class.name, :voter_id => user.id).first
与
替换此行:
votes.where(:voter_type => user.class.name, :voter_id => user.id).first
与
尝试重新启动服务器。我这样解决了。尝试重新启动服务器。我是这样解决的。这很有效,谢谢,但是我现在遇到了不同类型的错误ActionView::Template::error(找不到表“投票”):
@Cyzanfar您是否运行了acts asvotable gem附带的迁移?如果没有,您需要运行它。这很有效,但我现在收到不同类型的错误ActionView::Template::error(找不到表“投票”):
@Cyzanfar您是否运行了acts\u as\u votable gem随附的迁移?如果没有,您需要运行它。
votes_for.where(:voter_type => user.class.name, :voter_id => user.id).first