Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing vote_fu in rails app (or alternatives)
    primarykey
    data
    text
    <p>I've been trying 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.</p> <p>In a nutshell it just incremented a <code>votes_count</code> column in the <code>Answer</code> 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 spent the entire weekend tearing apart my application trying to get it to work.</p> <p>I 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:</p> <p>answers_controller.rb:</p> <pre><code>def vote_up answer = Answer.find(params[:id]) current_user.vote_up(answer), :voter_id =&gt; current_user.id redirect_to :back end </code></pre> <p>votes_controller.rb:</p> <pre><code>def create @quote = Answer.find(params[:answer_id]) respond_to do |format| if current_user.vote(@answer, params[:vote]) format.rjs { render :action =&gt; "create", :vote =&gt; @vote } format.html { redirect_to root_url } else format.rjs { render :action =&gt; "error" } format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @vote.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>answer.html.erb: (two different methods here, neither of these work)</p> <pre><code>&lt;span id="vote_form" style="float: right;"&gt; &lt;%= link_to "Vote up", :url =&gt; vote_up_answer_path(answer) %&gt; / &lt;%= link_to_remote "Down", : url =&gt; user_answer_votes_path(answer.user, answer, :vote =&gt; :false, :format =&gt; :rjs), :method =&gt; :post %&gt; &lt;/span&gt; &lt;span id="&lt;%= answer.id %&gt;_vote_score" class="vote_score"&gt; &lt;%= answer.votes_for - answer.votes_against %&gt; &lt;/span&gt; </code></pre> <p>routes.rb:</p> <pre><code>map.resources :users, :member =&gt; { :suspend =&gt; :put, :unsuspend =&gt; :put, :purge =&gt; :delete } do |user| user.resources :votes user.resources :answers do |answer| answer.resources :votes end end map.resources :answers, :has_many =&gt; :votes, :member =&gt; {:vote_up =&gt; :post, :vote_down =&gt; :post} </code></pre> <p>I am using Rails 2.3.5.</p> <p>Does anyone have any suggestions? Should I just go back to my old, handmade voting system? Is there another voting plugin or method I haven't heard of?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload