Sunday, December 16, 2007

Common rails validations



A rather short post, this one if focused on few validations in rails that I often use. In rails you could do a lot of validations, as they are very simple to handle. Because of the well structured and separated MVC architecture all you need to do is write you validations for the database in our model. Actually as a matter of fact you could also write them as a library and call its methods. The ones below aim to do just that. They use regular expressions, in case you are confused with what that is do read up.

Validating an Email Address

validates_format_of(:email,
:with => /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i,
:on => :create,
:message=>"has an invalid format")

Validating URLs

validates_format_of :url, :with => /^(https?://)?([^/]+)(/.+)?$/i

# OR

validates_format_of :url, :with => /^(ftp|https?)://((?:[-a-z0-9]+.)+[a-z]{2,})/

Validating IP addresses

validates_format_of :ip, :with => /^(d{1,3}.d{1,3}.d{1,3}.d{1,3})?$/

=================================================
||My FREE services||

ParkingWHOIS is now not only a domain parking lookup, but now also has reviews on Parking Companies.
=================================================


Tuesday, December 4, 2007

Installing Rails on uBuntu 7.10 Gusty Gibbons


So I wanted to install Ruby on Rails on my new linux box. And really thats not tough, this post just guides you to do the same. Actually I feel it is 80% easier to setup your linux box for any programming language than a windows COMPUTER. I might be wrong cause I used Windows for about a decade and then its a little more than one year since I have been using uBuntu (DUH!! a linux distro for the ones that didnt know) and I am already in love with it. One year and it still hasn't crashed. I can go on and on about my love for uBuntu but I think I will stop here. Ohh BTW uBuntu 7.10 (or uBuntu Gusty Gibbons has launched). It is sooooo light and awesome looking that my mom also is telling me to install it on her laptop. Truly linux for human beings. But I think I will let her laptop be a dual boot with XP and uBuntu; It will help me test my web apps.

Coming back to the main topic of discussion, I need to install Rails on uBuntu (actually works well for debian too). So here are few basic steps you might want to review:
1) Make sure all the build essentials are present on your Linux box. If not then quickly get into the terminal and hit

sudo apt-get install build-essential

2) Next you need to install ruby ri and rdocs so hit
sudo apt-get install ruby ri rdoc

3) Now you would want to download the latest versions of RubyGems; you could do that from http://rubyonrails.org/down or hit
sudo wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz

4) Uncompress and extract the file by
tar -xvzf rubygems-0.9.0.tgz

5) Go into that folder by
cd rubygems-0.9.0

6) execute the setup.rb file to setup RubyGems
sudo ruby setup.rb

7) Use RubyGems to install Rails
sudo gem install rails --include-dependencies

8) If the previous step gave you an error about SSL then install the Open SSL library for ruby
sudo apt-get install libopenssl-ruby


And you are ready to go!!! Lets test the Rails setup by creating a demo project and running it (remember you are still in the terminal)
$ rails testapp
$ cd testapp
$ ruby script/server

/testapp$ ./script/server
=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2006-12-07 05:28:37] INFO WEBrick 1.3.1
[2006-12-07 05:28:37] INFO ruby 1.8.4 (2005-12-24) [i486-linux]
[2006-12-07 05:28:37] INFO WEBrick::HTTPServer#start: pid=6687 port=3000



You’ll notice that the server is listening on port 3000. Open up a browser, and go to http://localhost:3000. Ok this is it for this article hope you are successful in setting up Rails on your Linux box and I wish your app gets as famous as David's. Peace.








=================================================
||My FREE services||

Find out more information about domain parking at ParkingWHOIS
=================================================