Created
August 7, 2014 04:33
-
-
Save bmneely/ee53f652d8dfd3b5e3ab to your computer and use it in GitHub Desktop.
votes_controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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