Rails IDEs - 3rdRails From CodeGear
September 23, 2007
I tried using Aptana and JEdit as IDEs for Rails development (to some success but not fully happy) before and recently tested CodeGear’s 3rdRail. I could see that 3rdRails is also designed based on Eclipse platform (same as case for Aptana Radrails). But what makes 3rdRail different from Aptana (other than $299 price tag) is couple of cool features like Logical representation of folders/files in project explorer. Controllers, Views are nicely tucked together. Models and Helpers are visible at top level makes it easy for you instead of grilling down the folders to get them.
Another neat feature is Console (they call it Project Commander), not only it shows output you could directly type and run all rails commands here (like rake tasks or installing plugins etc.)
Finally there is “Refactoring support”. For example, if you choose to rename an action, it will figure out all the files that action is referred and changes for you.
Go check it out at http://www.codegear.com/products/3rdrail. At least try for 3o days and see if you like it.
Performance Comparison For Rails and Merb
September 2, 2007
Merb is another MVC framework written in Ruby by Ezra Zygmuntowicz. Phil Misiowiec did some peformance comparsion for Rails and Merb when deploying with Mongrel, Swiftify and Nginx. You could look at full article at http://www.webficient.com/2007/08/testing-various-configurations-of-rails.html
For making it visually interesting, I created a simple Flex Chart based on those results. I kind of interpolated the same data in 2 different perspectives:
Chart 1 : Comparing Various Deployments for Rail & Merb
Chart 2 : Comparing Rails & Merb for Various Deployments
You could clearly see that Merb perfomed better than Rails but it is more evident when used with Evented Mongrel and less with simple Mongrel deployment. Also it showing storing sessions in MemCache is better than ActiveRecord store.
**** Note that I am not responsible for these results and full credit should go to Phil Misiowiec and I just try to represent the data visually here (while trying to play with Flex Charting) ******
Flex & Ruby on Rails
August 23, 2007
Recently I been looking into Adobe Flex and AIR and found promising. Flex been in market for couple of years and recently opensourced by Adobe (SDK is Free but FlexBuilder is NOT. But you could build Flex apps in any editor and compile with SDK). Flex Applications are run inside browser in Flash Player where as AIR makes applications build with flex run as desktop applications.
The good thing about Flash or Flex application is it looks and run identically in all browsers and environments where as similar thing is hard to achieve with pure DHTML or Ajax. Microsoft is also developing a similar product named “SilverLight” a browser plugin similar to Flash to run RIAs.
While all these are nice enhancement to Front-end, your backend still remains same. Flex/AIR/SilveLight can talk with Java, .NET or PHP backend services and present the data in more exciting way. Same as the case for Ruby on Rails.
In fact Rails makes serving your models as XML lot easier with ‘to_xml’ method and flexible functions like ‘respond_to?’ to sniff whether client wants HTML or XML. So it shouldnt be any harder to mix some flex in your app.
These are some resources to get your feet wet:
Ruby on Rails: Validation in Models
August 1, 2007
Validating incoming data to make sure data is according to business logic rules is important and is easy to do in Rails. The best place to get more information is Official Rails Documentation site (http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html) but if you want a general overview, proceed further.
As a data gatekeeper, Model class can be a perfect place to keep all your data validations. That way whatever way you create/modify data (programmatically or user interface) the validations work.
Basically we could write validations in 3 passes,
Pass 1: Use Rails inbuilt simple validation rules
a) Field should not be empty
Rails Method: validates_presence_of
Example: validates_presence_of :user_name, :password
b) Field should Numerical
Rails Method: validates_numericality_of
Example: validates_numericality_of :value
c) Field should not be Unique
Rails Method:validates_uniqueness_of
Example: validates_uniqueness_of :user_name
d) Field Need to be of size X
Rails Method: validates_length_of
Example: validates_length_of :state_code, :is=>2
e) Field size Need to be of between X to Y
Rails Method: validates_length_of
Example: validates_uniqueness_of :user_name
Pass 2 : Write Regular Expression to do validation
Requirement: Field Need to be in Certain Format like Email or DOB
Rails Method: validates_format_of
Example: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
(Tip : For all the above validations you could add :message => “Your customized message if validatation fails” to customize Error Messages)
Pass 3 : Custom Validations
Finally, for any other custom validations like say if product_price should be more than $100 or salary_requested less than 100K, define a protected method “validate” inside your model. Then use errors object to add any messages if validation fails like errors.add (:your_field, “your message”).
example from Book “Rails Solutions” By Justin Williams
def validate
errors.add(:price, “should be a positive value”) if price.nil?|| price < 0.01
end
(This is setup I comeup based on various online tutorials mainly : http://mongrel.rubyforge.org/docs/apache.html, http://tonyrose023.blogspot.com/2007/01/multiple-rails-apps-with-mongrel.html, http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/ )
Say you have multiple rails applicatons located at
/opt/myapp1 /opt/myapp2/ /opt/myapp3/ .....so on
and you want to run mongrel cluster for each application and want to access through http://www.yourhost.com/myapp1, http://www.yourhost.com/myapp2 using Apache
Mongrel Setup
Letus setup for myapp1 first then we work on others…. As this is our first setup, we need to do some groundwork
install mongrel and mongrel_cluster, if havent done yet.
$ gem install mongrel mongrel_cluster
Then for myapp1, configure mongrel cluster ( say we want to run 3 servers starting from port 8000 and only accessible from localhost…. public need to access from apache..we cover that later )
$ cd /opt/myapp1/
$ mongrel_rails cluster::configure -e production -p 8000 -N 3 -c /opt/myapp1 -a 127.0.0.1 —-prefix /myapp1
Make sure it working by starting cluster
$ mongrel_rails cluster::start
You should see your applicaton serving in at http://127.0.0.1:8000, http://127.0.0.1:8001 and http://127.0.0.1:8002
Stop it as we need to do some work on this
$ mongrel_rails cluster::stop
Then make this cluster start everytime your linux box restarts.
Make a folder in /etc/ called mongrel_cluster ( it needs to be exactly /etc/mongrel_cluster to make this work… if you choose to name it different then you need to change /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster file accordingly)
$ mkdir /etc/mongrel_cluster
Then copy or link our mongrel cluster configuration file as a unique name in that folder
$ ln -s /opt/myapp1/config/mongrel_cluster.yml /etc/mongrel_cluster/myapp1.yml
Now copy the provided cluster starting script from gems to /etc/init.d/ and give exutive permissons
$ cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/
$ chmod +x /etc/init.d/mongrel_cluster
$ /sbin/chkconfig –level 345 mongrel_cluster on
Thats it. now our cluster starts at boot time. For timebeing start manually, to make sure its working
$/etc/init.d/mongrel_cluster start
The nice thing about this script is, it looks for all configuration files in /etc/mongrel_cluster/ folder and parse those and start clusters defined in those config files.
So Now its obvious that for our remaining applications also to start at boot time, we need to copy the respective config files to /etc/mongrel_cluster/
So for myapp2 ( this time lets say we want 2 servers running starting from 8100 port)
$ cd /opt/myapp2/
$ mongrel_rails cluster::configure -e production -p 8100 -N 2 -c /opt/myapp2 -a 127.0.0.1 —-prefix /myapp2
$ ln -s /opt/myapp2/config/mongrel_cluster.yml /etc/mongrel_cluster/myapp2.yml
Same way do for remaining projects.
Apache Setup
Install Apache as usual but make sure to include mod_proxy, mod_rewrite, mod_proxy_balancer modules. See http://mongrel.rubyforge.org/docs/apache.html guideslines for possible configuration flags
#./configure --enable-deflate --enable-proxy --enable-proxy-html --enable-proxy-balancer --enable-rewrite --enable-cache --enable-mem-cache --enable-ssl --enable-headers
After Installatoin, find httpd.conf file in conf/ directory and at the bottom add this line
Include conf/rails.conf
rails.conf file looks like this
<VirtualHost *:80>
RewriteEngine On
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/myapp1(.*)$ balancer://myapp1%{REQUEST_URI} [P,QSA,L]
RewriteRule ^/myapp2(.*)$ balancer://myapp2%{REQUEST_URI} [P,QSA,L]
# …….add more RewriteRule s for other apps if needed
ErrorLog logs/rails_errors_log
CustomLog logs/rails_log combined
</VirtualHost><Proxy balancer://myapp1>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
</Proxy>
<Proxy balancer://myapp2>
BalancerMember http://127.0.0.1:8100
BalancerMember http://127.0.0.1:8101
</Proxy>
Here I added configuration for 2 apps. If you want add more apps, simply add RewriteRule and corresponding balancer to above file. This should make your apps serving at www.yourhost.com/myapp1 and www.yourhost.com/myapp2
Any Suggestions, comments, corrections are appreciated.
Installing Ruby, Rails and MySQL on Fedora Core 5
July 11, 2007
A) Installing Ruby
$ yum install ruby ruby-libs ruby-mode ruby-rdoc ruby-irb ruby-ri ruby-docs
B) Install Ruby Gems
Download rubygems-0.9.4.tgz from http://rubygems.org/ to /tmp folder
$ tar -xvfz /tmp/rubygems-0.9.4.tgz
$cd rubygems-0.9.4
$ ruby setup.rb
C) Install Rails
$ gem install rails
D) Install MySQL
Download From mysql-community server from http://dev.mysql.com/downloads/mysql/5.0.html
I downloaded mysql-5.0.41-linux-i686-icc-glibc23.tar.gz to /tmp folder
$ groupadd mysql
$ useradd -g mysql mysql
$ cd /usr/local
$ gunzip < /tmp/mysql-5.0.41-linux-i686-icc-glibc23.tar.gz | tar xvf -
$ ln -s mysql-5.0.41-linux-i686-icc-glibc23 mysql
$ cd mysql
$ chown -R mysql .
$ chgrp -R mysql .
$ scripts/mysql_install_db –user=mysql
$ chown -R root .
$ chown -R mysql data
(See http://dev.mysql.com/doc/refman/5.0/en/installing-binary.html instructions if you need detailed instructions)
Now to Automatically start server at boot
$ cp mysql.server /etc/init.d/mysql
$ chmod +x /etc/init.d/mysql
$ chkconfig –add mysql
(see http://dev.mysql.com/doc/refman/5.0/en/unix-post-installation.html for detailed instructions )
Finally remove anonymous user and set password for root user
$ mysql -u root
mysql> DELETE FROM mysql.user WHERE User = ”;
mysql> FLUSH PRIVILEGES;
mysql> UPDATE mysql.user SET Password = PASSWORD(’yournewpwd’) WHERE User = ‘root’;
mysql> FLUSH PRIVILEGES;
E) Installing mysql Gem
$ gem install mysql — –with-mysql-include=/usr/local/mysql/include –with-mysql-lib=/usr/local/mysql/lib
F) Create Rails Project and Makesure it works
$ rails testproject
Now add mysql password to config/database.yml file. And create respecive databases in mysql. I am assuming you, you didnt change database names in database.yml file so database names are testproject_development, testproject_test, testproject_production
$ mysql -u root -p
Enter your password
mysql > CREATE DATABASE testproject_development;
mysql > CREATE DATABASE testproject_test;
mysql > CREATE DATABASE testproject_production;
Now run cd to your project and see if this thing works
$ cd testproject
$ rake db:migrate
If you dont see errors, mean rails can see your databases.
Go ahead and develop Rails applications….
Build A Simple Registration Form Using Rails
May 30, 2007
In Rails you could simply build a Form based on model using “form_for” helper.
Assume your “users” table has login, password, name, email columns. Thats mapped as “User” Model using ActiveRecord (in app/models/user.rb file)
class User < ActiveRecord::Base
end
Now you could build a registration form (in views/register.rhtml file) as
<% form_for :user do |f| %>
<p> Your Name: <br /> <%= f.text_field :name %></p>
<p> Login ID:<br /><%= f.text_field :login %></p>
<p> Password: <br /> <%= f.text_field :password %></p>
<p> Email:<br /> <%= f.text_field :email %></p>
<p><%= submit_tag “Create Account” %></p>
<% end %>
To complete things, write controller logic to save data (in file apps/controllers/user_controller.rb)
class UserController < ApplicationController
def register
@user = User.new(params[:user])
if(request.post? and @user.save)
flash[:notice] = “Account Created Successfully”
redirect_to :controller => ‘yourloginsuccessfullycontrollername’
end
end
end
Now you could access registration page at http://yourhostname/user/register
Using NifyCube with Rails For Rounded Corners
May 29, 2007
Step 1: Download NiftCube from http://www.html.it/articoli/niftycube/NiftyCube.zip Basically You need only 2 files from this zip: niftycube.js and niftyCorners.css
Step2 : Copy niftycube.js to public/javascripts/ folder and niftyCorners.css to public/stylesheets/ folder in Rails
Step3: As we are keeping css file in different directory than js file You need to change a line in niftycube.js
Change line # 37 to l.setAttribute(”href”,”stylesheets/niftyCorners.css”); This enables niftycube to include the correct stylesheet onthe fly in rhtml. Or If you prefer an easy way, simply copy niftyCorners.css to public/javascrips/ folder too. In that way you dont need to change js file.
Step4: Include Js file using <%= javascript_include_tag “niftycube” %> in your rhtml file.
Step5: Finally in your javascript/application.js or page specific.js Add
window.onload = function(){
Nifty(”div#content,div#nav”);
}
Where content, nav are div IDs I want to make rounded corners. You could add as many as you want and choose whether you want rounded corners allsides or only some side. See http://www.html.it/articoli/niftycube/index.html for excellent documentation.
My hatsoff to Alessandro Fulciniti writing excellent NiftyCube library and ofcouse to Rails team.