I've been attempting to implement a robust voting system in rails for some time but have been struggling. Initially I built my own voting system, but it was simplistic. In a nutshell it just incremented a votes_count column in the "Answer" model using the counter cache...this worked fine for me for a while until I discovered vote_fu...and realized how much more robust my voting system could be. I immediately installed it and promptly spent the entire weekend tearing apart my application trying to get it to work. I've found a few other questions here on stack overflow related to this plugin, but none of them actually came up with a final solution. Here is my code:
//answers_controller.rb
def vote_up
answer = Answer.find(params[:id])
current_user.vote_up(answer), :voter_id => current_user.id
redirect_to :back
end
//votes_controller.rb
def create
@quote = Answer.find(params[:answer_id])
respond_to do |format|
if current_user.vote(@answer, params[:vote])
format.rjs { render :action => "create", :vote => @vote }
format.html { redirect_to root_url }
else
format.rjs { render :action => "error" }
format.html { render :action => "new" }
format.xml { render :xml => @vote.errors, :status => :unprocessable_entity }
end
end
end
//answer.html.erb (two different methods here, neither of these work)
<span id="vote_form" style="float: right;">
<%= link_to "Vote up", :url => vote_up_answer_path(answer) %>
/
<%= link_to_remote "Down", :
url => user_answer_votes_path(answer.user, answer, :vote => :false, :format => :rjs), :method => :post
%>
</span>
<span id="<%= answer.id %>_vote_score" class="vote_score">
<%= answer.votes_for - answer.votes_against %>
</span>
//routes.rb
map.resources :users, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete } do |user|
user.resources :votes
user.resources :answers do |answer|
answer.resources :votes
end
end
map.resources :answers, :has_many => :votes, :member => {:vote_up => :post, :vote_down => :post}
Sorry this post is so long.
I have no idea why this isn't working, for all intents and purposes it should. I am using Rails 2.3.5 btw.
Does anyone have any suggestions? Should I just got back to my old, handmade voting system? Is there another voting plugin or method I haven't heard of? Cheers.