Showing posts with label Ruby on Rails. Show all posts

Implementing live validation in Ruby on Rails

by Noman Muhammad

LiveValidation is a small open source javascript library for making client-side validation quick, easy, and powerful. livevalidation plugin is used to integrate Javascript library LiveValidation to the Rails application. LiveValidation implements client-side form validation. To see livevalidation in action ( !!! ) we have to install it in our rails project. Then use Rails validation helpers to validate our models and use Rails form helpers to create our forms.

here is the example steps how i make it live in my project. [i use Netbeans 6.0 to build my app]

1. create a new project
2. run Rake Task to create the db
3. generate scaffold with this parameter in the [Model Name:] text field

Blog title:string body:text
4. now, download livevalidation plugin from github
5. unzip the downloaded zip folder and rename the folder 'livevalidation'
6. store 'livevalidation' folder in the "\vendor\plugins\" folder. [i.e. \vendor\plugins\livevalidation]
7. now install Javascript and CSS with this Rake task
$ rake livevalidation:install

8. add these lines in /views/layout/blogs.html.erb
<%= javascript_include_tag :defaults, 'live_validation' %>
<%= stylesheet_link_tag 'live_validation' %>
9. now add validations to the model
validates_presence_of :title
10. now start the server and go to http://localhost:3000/blogs/new
11. when you leave title textfield without entering any value, you will get an error in RED and when you type something in that field you will get a thnx :) . so its working

Ruby on Rails installation in Ubuntu

by Noman Muhammad

i'm using Ubuntu 8.10. here is the steps how i install RoR in ubuntu. i get this instruction from Ubuntu Community documentation. I just mention here the steps that i do to install RoR.

1. at first open the terminal and to get Ruby run this command

sudo apt-get install ruby-full build-essential
this command also install a web server named 'WEBrick'

2. Now to download and install ruby gems we to run the following command one after another
wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
tar xzvf rubygems-1.3.1.tgz
cd rubygems-1.3.1
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system
this will install Rubygems 1.3.1

3. then we install RAILS as gem
sudo gem install rails
4. so, RoR is installed in Ubuntu.

5. now we will install MySQL database and it's connector
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql
6. now to create a new rails application run this command
rails /home/www/test_app -d mysql
this will create a new applicaton named 'test_app' in '/home/www' directory

7. now to run the application do the following
$cd /home/www/test_app
$ruby script/server

then go to open the browser and go to 'http://0.0.0.0:3000'. new application will respond

Ruby on rails installation on Windows

by Noman Muhammad

a. First, let's check to see if you already have Ruby installed. Bring up a command prompt and and type ruby -v. If Ruby responds, and if it shows a version number at or above 1.8.2 then type gem --version. If you don.t get an error, skip to step 3. Otherwise, we.ll install a fresh Ruby.
b. If Ruby is not installed, then download an installation package from rubyinstaller.rubyforge.org. Follow the download link, and run the resulting installer. This is an exe like ruby186-25.exe and will be installed in a single click. You may as well install everything . It's a very small package, and you'll get RubyGems as well alongwith this package. Please check Release Notes for more detail.
c. With RubyGems loaded, you can install all of Rails and its dependencies through the command line:

C:\> gem install rails --include-dependencies
Keeping Rails Up-to-Date:
Assuming you installed Rails using RubyGems, keeping up-to-date is relatively easy. Issue the following command:
C:\> gem update rails
This will automatically update your Rails installation. The next time you restart your application it will pick up this latest version of Rails. While giving this command, make sure you are connected to the internet.

Installation Verification
You can verify if everything is setup according to your requirements or not. Use the following command to create a demo project.
C:\> rails demo
This will generate a demo rail project, we will discuss about it later. Currently we have to check if envrionment is setup or not. Now next use the following command to run WEBrick web server on your machine.

C:\> cd demo
C:\demo> ruby script/server
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-02-26 09:16:43] INFO WEBrick 1.3.1
[2007-02-26 09:16:43] INFO ruby 1.8.2 (2004-08-24)...
[2007-02-26 09:16:43] INFO WEBrick::HTTPServer-start:pid=2836...
....
Now open your browser and type the following address text box.
http://localhost:3000