Skip to content

Instantly share code, notes, and snippets.

<script type="text/javascript">
function validate_contact_admin_form(form_id) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements['email'].value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}
</script>
@unnitallman
unnitallman / gist:944011
Created April 27, 2011 10:11
sqlite with activerecord outside rails
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => ":memory:"
)
@unnitallman
unnitallman / gist:1010056
Created June 6, 2011 10:36
Setting a cookie with an expiry time
cookies[:survey_user_id] = { :value => self.current_user.id.to_s, :expires => 1.year.from_now }
@unnitallman
unnitallman / gist:1153108
Created August 18, 2011 01:47
FTP with Ruby
require 'net/ftp'
ftp=Net::FTP.new
ftp.connect(URL,21)
ftp.login(username,passwd)
ftp.chdir(directory)
ftp.getbinaryfile(filename)
ftp.close
http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/index.html
@unnitallman
unnitallman / gist:1188060
Created September 2, 2011 06:50
Specifying rails version to use when creating a new application
rails _2.2.2_ myapp
@unnitallman
unnitallman / connection_fix.rb
Created September 3, 2011 09:13 — forked from defunkt/connection_fix.rb
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@unnitallman
unnitallman / gist:1237177
Created September 23, 2011 11:47
Typecasting a base class object to a derived class object
u = User.find(id)
u = u.becomes(u.type.constantize)
@unnitallman
unnitallman / gist:1247809
Created September 28, 2011 12:28
Adding query string to a url
>> callback = Addressable::URI.parse("http://unni.com")
=> #<Addressable::URI:0x810ab0b4 URI:http://unni.com>
>> callback.query_values = callback.query_values ? callback.query_values.merge({"angry" => "birds"}) : {"angry" => "birds"}
=> {"angry"=>"birds"}
>> callback.to_s
=> "http://unni.com?angry=birds"
>> callback = Addressable::URI.parse("http://unni.com?nikhil=krishna")
=> #<Addressable::URI:0x8109fc8c URI:http://unni.com?nikhil=krishna>
>> callback.query_values = callback.query_values ? callback.query_values.merge({"angry" => "birds"}) : {"angry" => "birds"}
@unnitallman
unnitallman / gist:1266826
Created October 6, 2011 08:17
Downgrading rubygems
sudo gem update --system 1.3.5
@unnitallman
unnitallman / gist:1280308
Created October 12, 2011 04:47
Use a specific rubygems version with RVM
rvm rubygems 1.3.5