Setup multistage deployment with Capistrano Passenger and SVN with Staging Development Production environments

When application needs to be set on different environment like staging and development question comes  how to manage cap deploy ?

Here are easy steps which will guide to setup and manage with SVN and Capistrano

Setup multistage deployment with Capistrano
1)    Install capistrano and capistrano multistage
2)    Capify your project
3)    Prepare the deploy / directory
4)    Create the deployment recipes

For my project i have created development and production environment. Name your stages whatever you want. but, never name one of your stages stage — it’s a reserved word. We Can name it “staging”.

1) Install capistrano and capistrano multistage

gem install capistrano –no-ri –no-rdoc
gem install capistrano-ext –no-ri –no-rdoc

2) Capify your project

capify you project with following command in project root folder

capify .

3) Prepare the deploy / directory

mkdir config/deploy
touch config/deploy/development.rb
touch config/deploy/production.rb

4) Create the deployment recipes

require ‘capistrano/ext/multistage’

set :stages, %w(development production)
set :default_stage, “development”
set :application, “application”

set :user, “username”
set :password, “password”

set :repository, “https://domain/svn/project/trunk”
set :scm, :subversion
set :scm_username, “repository_username”
set :scm_username, “repository_password”
set :deploy_via, :export

# deploy using “ssh-add”, then “cap deploy”
ssh_options[:forward_agent] = true
set :ssh_options, {:forward_agent => true}
set :use_sudo, false

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
task :start do ; end
task :stop do ; end

task :install_configs
#perform specify operations like set path or other stuff here

task :restart, :roles => :app, :except => { :no_release => true } do
run “#{try_sudo} touch #{File.join(current_path,’tmp’,’restart.txt’)}”

after(‘deploy:update_code’, “deploy:cleanup”, ‘deploy:install_configs’)


Now it’s turn to set both files which we have created in config/deploy 1) development.rb  2) production.rb

Set your development.rb

server “”, :app, :web, :db, :primary => true
set :deploy_to, “/var/www/” #path where you want to deploy application on server
set :db_config, “database-development.yml”

namespace :perform do
task :install_configs
#perform specify operations like set path or other stuff here

after(“deploy”, “perform:install_configs”)

Set your production.rb

server “”, :app, :web, :db, :primary => true
set :deploy_to, “/var/www/” #path where you want to deploy application on server
set :db_config, “database-production.yml”

namespace :perform do
task :install_configs
#perform specify operations like set path or other stuff here

after(“deploy”, “perform:install_configs”)


Great, you are ready to deploy with both environments

When you deploy recipe with cap deploy it will start deploying default environment whatever is set, if you want to deploy specific for environment please run command cap “environment” deploy in our case cap production deploy .

All set with multistage Capistrano deployment with minimal configuration.Lot more for rails 3 and RVM configuration.

Setup Ubuntu machine for Ruby on Rails with RVM-Mysql-Git-Curl-Java-Sublime Text 2 or Scite

Configure brand new Ubuntu machine for Rails

Step 1 – Install RVM

Prerequisite for RVM sudo apt-get install curl git-core

Run command in terminal bash -s stable < <(curl -s )

After installation update .bashrc is require

command >nano ~/.bashrc

Replace [ -z "$PS1" ] && return with if [[ -n "$PS1" ]]; then

Also add following line of code at end of file

if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source $HOME/.rvm/scripts/rvm ; fi


Run bash -l in terminal.

Now rvm notes you can see rvm is working !

Install following packages before installing ruby via RVM

sudo apt-get install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf

After that rvm list known will display know ruby package choose your favorite !

Install using e.g rvm install 1.9.2

RVM Up and Running !!!!!!

Step 2 – Install Mysql

apt-get install mysql-server

Step 3 – Install GIT and Curl

Already installed as prerequisite.

Step 4 – Install Java

Can install Java using two methods

1st Method

sudo apt-cache search jdk
sudo apt-get install sun-java6-jdk sun-java6-jre

and set export JAVA_HOME=”/usr/lib/jvm/java-6-sun-″ in your .bashrc file

2nd Method (Live installation)

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:sun-java-community-team/sun-java6
sudo apt-get update
sudo apt-get install sun-java6-jdk

System is ready with java, can check version with java –version in terminal

Step 5 – Install Sublime Text 2

Add Sublime Text 2 PPA using the following command

sudo add-apt-repository ppa:webupd8team/sublime-text-2

After run these commands in terminal
sudo apt-get update
sudo apt-get install sublime-text-2-beta

Step 6 – Install Scite

Run following command in terminal

sudo apt-get install scite

After this installations you are ready to enjoy rails ……

Simple way to validate email+domain with Ruby on Rails

require ‘resolv’ #”gem install resolv-ipv6favor

email = “” #test with valid email
split_email = email.split(“@”) #split string with “@”
domain = split_email[1].to_s #get domain name

if email =~ (/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
#checking domain name do |dns|
@staus = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
errors_add(:email, ‘domain name can not be found.’)

Resolve Cookie overflow in rails

Sometimes we need long flash notice message which is unable to handle by rails and yields cookies overflow error to over come this  patch with this method.

put this …

before filter :make_session_clear

def make_session_clear
rollback =
rollback.each { |k, v| session[k] = v unless k.is_a?(String) && k =~ /^as:/ }

That’s it you are up and running…..

Simple Tooltip with Ruby on Rails

1st Step

add this code in app/helpers/application_helper.rb

def tooltip(content, options = {}, html_options = {}, *parameters_for_method_reference)
html_options[:title] = options[:tooltip]
html_options[:class] = html_options[:class] || ‘tooltip’
content_tag(“span”, content, html_options)

2nd Step

add code in app/views/foo/page.rhtml

3rd Step

add stylesheet in public/stylesheets/yourstyle.css

.mytooltip {
cursor: help;
color: green;
border-bottom: 1px dotted green

What Open source software ?

Open source software (OSS) began as a marketing campaign for free software. OSS can be defined as computer software for which the human-readable source code is made available under a copyright license (or arrangement such as the public domain) that meets the Open Source Definition. This permits users to use, change, and improve the software, and to redistribute it in modified or unmodified form. It is very often developed in a public, collaborative manner. Open source software is the most prominent example of open source development and often compared to user generated content.

Simple Remember me functionality in Ruby on Rails with expiration date .

I have gone through ton of code for completing remember me functionality, i sink every time ,last i decided to code my own and i got it. Now you can use it.

You need to make changes only in two files.
1>app/controllers/*.rb (eg. account.rb)
2>app/views/your directory/*.rhtml (eg. login.rhtml)

before_filter :loginformcookies

def loginformcookies
if !cookies[:remember_me_id].blank? && !
if ( User.find(cookies[:remember_me_id]) and Digest::SHA1.hexdigest(User.find(cookies[:remember_me_id]).email)[4,18] == cookies[:remember_me_code])

def login
case request.method
when :post
if  session[:user] = User.authenticate(params[:user_login], params[:user_password])
if params[:rememberme]
userId = session[:user][:id].to_s
cookies[:remember_me_id] = { :value => userId, :expires => 30.days.from_now }
userCode = Digest::SHA1.hexdigest(session[:user][:email])[4,18]
cookies[:remember_me_pass] = {:value =>params[:user_password],  :expires => 10.days.from_now}
cookies[:remember_me_code] = {:value =>userCode, :expires => 10.days.from_now}
cookies.delete :remember_me_pass
cookies.delete :remember_me_code
cookies.delete :remember_me_id
flash[:notice]  = “Login successful”
cookies[:is_admin] = “yes”
redirect_back_or_default :controller => “admin/general”, :action => “index”
else[‘notice’]  = “Login unsuccessful”

@login = params[:user_login]

In view file just pass that variable which you are getting in filter (loginformcookies).

and have fun.