Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 0.51 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # app/models/vote.rb
  2. class Vote < ActiveRecord::Base
  3.   belongs_to :post
  4.   validates_presence_of :user_ip, :post
  5. end
  6.  
  7. class Post < ActiveRecord::Base
  8.   has_many :votes
  9.  
  10.   def voted_from?(ip)
  11.     !self.votes.where(:user_ip => @ip).empty?
  12.   end
  13. end
  14.  
  15. # app/controllers/votes_controller.rb
  16. class VotesController < ApplicationController
  17.   def create
  18.     ip = request.remote_ip
  19.     post = Post.find(params[:id])
  20.  
  21.     unless post.voted_from?(params[@ip])
  22.       Vote.create(params[:vote])
  23.     end
  24.   end
  25. end