简体   繁体   中英

Custom Rails Validation (Doesn't seem to be working)

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&type=prem&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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM