试图将业力添加到acts_as_votable

时间:2016-08-18 00:44:36

标签: ruby-on-rails ruby-on-rails-4 acts-as-votable

我试图将业力添加到acts_as_votable。丢失!请帮忙。

基本上,我对文章进行了上/下投票。

每当他们的文章被投票时,我想向发布商业力添加1。 (当一个被投票时减去1)。因此,简而言之,当某人对某篇文章进行投票时,该文章获得了投票,而出版商获得了业力点。

我投票的文章工作得很好。

我遵循了本教程 (https://masteruby.github.io/weekly-rails/2014/08/12/how-to-add-user-karma-to-rails-app.html) 尝试在文章投票时向发布者添加业力,但我不断在日志中收到此错误。

NoMethodError - undefined method `increase_karma' for #<Publisher:0x000001055d6f00>

我已经多次运行迁移并重新启动服务器。

在我的文章控制器中(我也使用friendly_id)

def upvote
  @article = Article.find_by_slug(params[:id])
  @article.upvote_by current_user
  @article.publisher.increase_karma
  respond_to do |format|
    format.html { redirect_to :back }
    format.js { render layout: false }
  end
end

在我的发布商控制器

  def increase_karma(count=1)
    update_attribute(:karma, karma + count)
  end

  def decrease_karma(count=1)
    update_attribute(:karma, karma - count)
  end

1 个答案:

答案 0 :(得分:1)

Rails抛出错误的原因是因为您的发布商模型没有increase_karma方法。

increase_karmadecrease_karma方法应该从Publisher控制器转移到Publisher模型,它应该消除错误。

  def increase_karma(count=1)
    update_attribute(:karma, karma + count)
  end

  def decrease_karma(count=1)
    update_attribute(:karma, karma - count)
  end