Topic: Voting Tutorial...

I need a tutorial on creating a voting setup on my site. I would like the user to just click the vote button, and it will add the number of votes to the item they are voting on.

similiar to this rails site: http://www.designbyhumans.com/vote?page=1

Is there plug-ins I can get from git?

Thanks Again.

Re: Voting Tutorial...

It's a bit dirty, and someone can probably tell you how to do it better, but here you go.
As you see the votes will work without the page having to reload.

table item_votes
id
item_id
user_id
value - boolean

items table
name
description, and so on
total_value - integer DEFAULT 0

ItemVote controller
def vote_up
check = ItemVote.find(:first, :conditions => ["user_id = ? AND item_id = ?", session[:user_id], params[:id]])
item = Item.find(params[:id])
if check.nil?
item_vote = ItemVote.new
item_vote.item_id = params[:id]
item_vote.user_id = session[:user_id]
item_vote.value = true
item_vote.save
item.total_value += 1
item.save
render :text => item.total_value
elsif check.value == false
check.value = true
check.save
item.total_value += 2
item.save
render :text => item.total_value
else
render :text => "You have already voted up for this item."
end
end

def vote_down
check = ItemVote.find(:first, :conditions => ["user_id = ? AND item_id = ?", session[:user_id], params[:id]])
item = Item.find(params[:id])
if check.nil?
item_vote = ItemVote.new
item_vote.item_id = params[:id]
item_vote.user_id = session[:user_id]
item_vote.value = true
item_vote.save
item.total_value -= 1
item.save
render :text => item.total_value
elsif check.value == true
check.value = false
check.save
item.total_value -= 2
item.save
render :text => item.total_value
else
render :text => "You have already voted down for this item."
end
end

Item controller

def list
@items = Item.find(:all, :limit => 50, :order => [" name "])
end

list.rhtml

<% for i in @items %>
<%= i.title %>
<%= i.description %>
<div id="total_value_<%= i.id %>"><%= i.total_value %></div>
<%= link_to_remote "Vote up", :url => {:controller => :item_votes, :action => :vote_up, :id => i.id}, :update => "total_value_#{i.id}" %>
<%= link_to_remote "Vote down", :url => {:controller => :item_votes, :action => :vote_down, :id => i.id}, :update => "total_value_#{i.id}" %>
<% end %>

Re: Voting Tutorial...

thanks for this.