Im trying to add karma to acts_as_votable. Lost! Please help.
Basically I have a up/down vote working on the Articles.
I would like to add 1 to publishers karma each time their article is upvoted. (and subtract 1 when one is downvoted). So in a nutshell when someone votes on an article the article gets a vote and the publisher gets a karma point.
I have the voting of articles working just fine.
I followed this tutorial (https://masteruby.github.io/weekly-rails/2014/08/12/how-to-add-user-karma-to-rails-app.html) to try to implement adding karma to publishers when an article is voted on but I keep getting this error in the logs.
NoMethodError - undefined method `increase_karma' for #<Publisher:0x000001055d6f00>
I've run the migrations and restarted server multiple times.
In my Article Controller (Im also using 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
In my Publishers Controller
def increase_karma(count=1)
update_attribute(:karma, karma + count)
end
def decrease_karma(count=1)
update_attribute(:karma, karma - count)
end
The reason why Rails is throwing an error is because your Publisher Model doesn't have a increase_karma
method.
increase_karma
and decrease_karma
methods should be moved to the Publisher
model from the Publisher
Controller and it should eliminate the error.
def increase_karma(count=1)
update_attribute(:karma, karma + count)
end
def decrease_karma(count=1)
update_attribute(:karma, karma - count)
end