1
votes

I found this tutorial about how to build a simple voting system.

I already have a User and Post model (just included the relevant columns) and I'm using Devise:

  create_table "posts", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.string   "title"
    t.integer  "total_value",    :default => 0 // Added by following the tutorial 
  end

  create_table "users", :force => true do |t|
    t.string   "username"
  end

post.rb:

class Post < ActiveRecord::Base
  attr_accessible :title, :content

  belongs_to :user
  has_many :comments, :dependent => :destroy
  has_many :votes, :dependent => :destroy
end

user.rb:

class User < ActiveRecord::Base    
  has_many :posts, :dependent => :destroy
  has_many :comments, :dependent => :destroy   
end

vote.rb:

class Vote < ActiveRecord::Base
  belongs_to :post
end

migration:

class CreateVotes < ActiveRecord::Migration
  def change
    create_table :votes do |t|
      t.integer :post_id
      t.integer :user_id
      t.boolean :value

      t.timestamps
    end

    add_index :votes, [:post_id, :user_id]
  end
end

votes_controller.rb:

class VotesController < ApplicationController
  def vote_up
    check = Votes.find(:first,
                       :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]])

    post = Post.find(params[:id])

    if check.nil?
      vote = Votes.new
      vote.post_id = params[:id]
      vote.user_id = session[:user_id]
      vote.value = true
      vote.save
      post.total_value += 1
      post.save
      render :text => post.total_value
    elsif check.value == false
      check.value = true
      check.save
      post.total_value += 2
      post.save
      render :text => post.total_value
    else
      render :text => "You have already voted up for this post."
    end
  end

  def vote_down
    check = Vote.find(:first,
                      :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]])

    post = Post.find(params[:id])

    if check.nil?
      vote = Vote.new
      vote.post_id = params[:id]
      vote.user_id = session[:user_id]
      vote.value = true
      vote.save
      post.total_value -= 1
      post.save
      render :text => post.total_value
    elsif check.value == true
      check.value = false
      check.save
      post.total_value -= 2
      post.save
      render :text => post.total_value
    else
      render :text => "You have already voted down for this post."
    end
  end
end

views/pages/index.html.erb:

<% for i in @posts %>
  <h2><%= i.title %></h2>
  <p><%= i.content %></p>

  <div id="total_value_<%= i.id %>"><%= i.total_value %></div>

  <%= link_to "Vote up",   :url => {:controller => :votes, :action => :vote_up, :id => i.id},
                           :update => "total_value_#{i.id}",
                           :remote => true %>
  <%= link_to "Vote down", :url => {:controller => :votes, :action => :vote_down, :id => i.id},
                           :update => "total_value_#{i.id}", 
                           :remote => true %>
<% end %>

Everything displays, there are no errors, but when I click vote up or vote down, absolutely nothing happens.

Any suggestions to fix this?

EDIT:

I don't see any error nor saving/creating messages in the terminal just stuff like this:

Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2012-02-01 06:47:50 +0800 Served asset /application.js - 304 Not Modified (0ms) [2012-02-01 06:47:50] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

1
Check Webrick server log in console. Is vote saved? Do you see it in sql query?Johny
@Johny Nothing happens. The terminal just shows stuff like : Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2012-02-01 06:47:50 +0800alexchenco
@alexchenco how did you solve the problem?LightBox
@Okyekyein Ha I wrote that voting system long, long time ago, so I don't even remember. Now I wrote another one.alexchenco
Do you mind sharing your new implementation? I'm trying to build a vote system myself. How accurate is the VotesController in stackoverflow.com/questions/9148788/…LightBox

1 Answers

1
votes

It is probably a typo but shouldn't you have, Vote.new, Vote.find,

Instead you have Votes.find and Votes.new

class VotesController < ApplicationController
  def vote_up
    check = Votes.find(:first,
                       :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]])

    post = Post.find(params[:id])

    if check.nil?
      vote = Votes.new
      vote.post_id = params[:id]
      vote.user_id = session[:user_id]
      vote.value = true
      vote.save
      post.total_value += 1
      post.save
      render :text => post.total_value
    elsif check.value == false
      check.value = true
      check.save
      post.total_value += 2
      post.save
      render :text => post.total_value
    else
      render :text => "You have already voted up for this post."
    end
  end

  def vote_down
    check = Vote.find(:first,
                      :conditions => ["user_id = ? AND post_id = ?", session[:user_id], params[:id]])

    post = Post.find(params[:id])

    if check.nil?
      vote = Vote.new
      vote.post_id = params[:id]
      vote.user_id = session[:user_id]
      vote.value = true
      vote.save
      post.total_value -= 1
      post.save
      render :text => post.total_value
    elsif check.value == true
      check.value = false
      check.save
      post.total_value -= 2
      post.save
      render :text => post.total_value
    else
      render :text => "You have already voted down for this post."
    end
  end
end

Change that to Vote.new and Vote.find and see.

UPDATE:

I suggest you use the debugger gem. Another suggestion is to use a gem for this functionality. There are tons of gems created for this. vote_fu and acts_as_votable are two most used.