views:

39

answers:

1

Hey guys ive got the following Model for Accounts

require 'net/http'
require 'uri'
require 'date'

class Account < ActiveRecord::Base
validates_presence_of :username, :password, :on => :update
validate :valid_expiry_date, :on => :update  

def valid_expiry_date
   reply = Net::HTTP.get URI.parse("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=getaccountdetails_v1&amp;type=prem&amp;login=" + username + "&password=" + password)
   account = Time.at(reply[80..90].to_i)

   if (Time.now + 2419200) <= account
     return true      
   else
     return false
     errors.add_to_base("Sorry this account isnt valid") 
   end
 end
end

I know the code works in a ruby.rb file and will return true or false, however I seem to be having rather a lot of difficultly trying to translate this code into an actual validation, any help would be much appreciated. Thanks :)

Its also defiantly at least connecting to the website as my firewall asked me if the Terminal was aloud to access it.

At the moment not only is it displaying no errors its actually letting anything through and saving it.

+2  A: 

I don't know it solved your problem or not but your 'return statement' should be after the line

   errors.add_to_base("Sorry this account isnt valid") 

Change it to and check again

   if (Time.now + 2419200) <= account
     return true      
   else
     errors.add_to_base("Sorry this account isnt valid") 
     return false
   end
Salil
Thank-you very much, its now working perfectly.
Karl Entwistle