Skip to content

Instantly share code, notes, and snippets.

@bmneely
Created August 7, 2014 04:33
Show Gist options
  • Save bmneely/ee53f652d8dfd3b5e3ab to your computer and use it in GitHub Desktop.
Save bmneely/ee53f652d8dfd3b5e3ab to your computer and use it in GitHub Desktop.
votes_controller
class VotesController < ApplicationController
before_action :load_post_and_vote
def update_vote(new_value)
if @vote
authorize @vote, :update?
@vote.update_attribute(:value, new_value)
else
@vote = current_user.votes.build(value: new_value, post: @post)
authorize @vote, :create?
@vote.save
end
end
private
def load_post_and_vote
@post = Post.find(params[:post_id])
@vote = @post.votes.where(user_id: current_user.id).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment