Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:54:01+00:00 2026-05-28T19:54:01+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:54:03+00:00Added an answer on May 28, 2026 at 7:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just found this great tutorial as it is something that I need.
I am trying to imitate this tutorial about creating a simple Apple-themed breadcrumb found
I found this tutorial about ffmpeg the thing i do not get is how
I found this tutorial about Activex component but I am getting an error under
I'm working my way through this tutorial here about progress dialogues but have hit
I found this sample tutorial The clueless guide to Hello World in nasm about
I'm running through this tutorial found here: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htm which teaches how to pass a
So I found this: http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html Here is the example: <definition name=myapp.homepage template=/layouts/classic.jsp> <put-attribute name=title
Below is some html I found in this jquery tooltip tutorial, the contents inside
I have a compiling problem. I followed tutorial about Hello world program for Android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.