views:

206

answers:

2

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.

+1  A: 

Hi Ryan,

I think the first step will be to get it working through the console. From the last error in your comment, it seems maybe your models are not set up correctly in your User model you should have the following

#In User.rb
acts_as_voter

#In Answer
acts_as_votable

Also make sure you have applied rake:migrate to your db. Then try in the console:

u = User.first 
a = Answer.last 
u.votes_for(a)

When I have used this in the past I would have a controller action that looks like the following:

  def vote
    @question = Question.find(params[:id])
    if params[:vote] == 'up'      
      current_user.vote_for(@question)
    elsif params[:vote] == 'down'      
      current_user.vote_against(@question)
    end
    redirect_to @question
  end

Hope this helps.

-- jt

Jonathan
A: 

Is this question closed, has there been a solution?

MrThomas
ended up just building my own voting system....I couldn't get it to work no matter what i did.
Ryan Max