Why doesn't this Ruby on Rails code work as I intend it to?
Posted
by
Justin Meltzer
on Stack Overflow
See other posts from Stack Overflow
or by Justin Meltzer
Published on 2011-03-19T08:03:58Z
Indexed on
2011/03/19
8:10 UTC
Read the original article
Hit count: 390
So I attempted to build what I asked about in this question: Fix voting mechanism
However, this solution doesn't work. A user can still vote however many times he or she wants. How could I fix this and/or refactor?
def create
@video = Video.find(params[:video_id])
@vote = @video.video_votes.new
@vote.user = current_user
if params[:type] == "up"
@vote.value = 1
else
@vote.value = -1
end
if @previous_vote.nil?
if @vote.save
respond_to do |format|
format.html { redirect_to @video }
format.js
end
else
respond_to do |format|
format.html { redirect_to @video }
format.js {render 'fail_create.js.erb'}
end
end
elsif @previous_vote.value == params[:type]
@previous_vote.destroy
else
@previous_vote.destroy
if @vote.save
respond_to do |format|
format.html { redirect_to @video }
format.js
end
else
respond_to do |format|
format.html { redirect_to @video }
format.js {render 'fail_create.js.erb'}
end
end
end
@previous_vote = VideoVote.where(:video_id => params[:video_id], :user_id => current_user.id).first
end
© Stack Overflow or respective owner