Getting started with Ruby on Rails

Video demonstration of ruby on rails application creation:

One of the many advantages of rails is that it immediately gets you from zero to a functional application. To get started lets create a directory for the rails project called “railsproject”.

mkdir railsproject

Now open the ruby command prompt and navigate to the directory that we just created. Run the rails new command to make your first rails application. Let us call our first rails application as “first”. The rails new command is a program that creates a skeleton rails application. It creates a standard file and directory structure that makes it easier than ever to organize and deploy files. All the rails application have a common file and directory structure making it easy to understand someone else’s code.

rails new first

After the file creation is done rails automatically runs the bundle install command which we will discuss later in this article. Upon successful execution of the rails new command, it will display the message “Your bundle is complete!”

Output:

Bundle install command

 

Now lets have a quick look into the files and directories that have just been created. I use Aptana studio, an IDE for windows users of Rails.

Rails application directory structure

 

Let’s learn more about the files that have been created.

  • app – This contains the Core application code including the models, views, controllers and helpers. The model, view and controller are very important parts of a rails architecture which we would learn in-depth in the later articles
  • script/rails -A script to generate code and start the local server
  • vendor – This contains third party code such as plugins and gems
  • Gemfile – This file contains the gem requirements of the application

The bundle install or the bundler command which we had seen in the beginning of this article installs the gems from the Gemfile. The bundler looks for the gems that needs to be installed from this file. Thus the Gemfil should contain the gems needed by your application.

Gemfile:

source 'https://rubygems.org'
 
gem 'rails', '3.2.8'
 
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
 
gem 'sqlite3'
 
# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
 
  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby
 
  gem 'uglifier', '>= 1.0.3'
end
 
gem 'jquery-rails'
 
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
 
# To use Jbuilder templates for JSON
# gem 'jbuilder'
 
# Use unicorn as the app server
# gem 'unicorn'
 
# Deploy with Capistrano
# gem 'capistrano'
 
# To use debugger
# gem 'debugger'

Inside the gemfile, the rails gem is the gem for rails itself. “sqlite3″ is the gem for the ruby interface to the SQLite database. ”jquery-rails”  is for the jQuery javascript library. As you could notice there is a version 3.2.8 specified in the rails gem command. Unless the version is explicitly specified, rails automatically deploys the latest version of that gem.

Hosting rails application on heroku

Signup for a Heroku account at https://api.heroku.com/signup

Then install heroku gem with the below command:

sudo gem install heroku

Remove ruby 1.9.2, install zlib and openssl package and then re-install ruby as follows:

rvm remove 1.9.2
rvm package install zlib
rvm package install openssl
rvm install 1.9.2 -C --with-zlib-dir=$HOME/.rvm/usr --with-openssl-dir=$HOME/.rvm/usr
rvm --default use 1.9.2@rails3tutorial

Install and compile readline with the below commands:

sudo apt-get install libncurses5-dev
sudo apt-get install libreadline5-dev
cd ~/.rvm/src/ruby-1.9.2-p180/ext/readline/
ruby extconf.rb
make
make install

Associate your Heroku account with the public key.

heroku keys:add

Use create command to create a heroku subdomain for the app:

heroku create

Using git to push the application to the heroku server:

git push heroku master

The open command opens up the url in a new browser window.

heroku open

RVM – Easy installation of ‘ruby on rails’ on ubuntu

Ruby Version Manager (RVM) resolves versions conflicts when multiple versions of ruby is installed on the same system. It also enables quick and easy installation of ruby on rails on ubuntu. This article provides solution to the problems faced during the installation of ruby on rails on ubuntu using RVM.

Installation of RVM:

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
source ~/.rvm/scripts/rvm
rvm reload

Install Ruby 1.9.2 with rvm:

rvm package install zlib
rvm install 1.9.2 -C --with-zlib-dir=$rvm_path/usr
rvm install 1.8.7-p174 -C –with-zlib-dir=$rvm_path/usr

To resolve version conflict, create separate gemsets for each of the two different versions:

rvm --create 1.8.7-p174@rails2tutorial
rvm --create use 1.9.2@rails3tutorial

To use Ruby 1.9.2 by default and Rails 3.0 give the below command:

rvm --default use 1.9.2@rails3tutorial

Install Rails:

gem install --no-ri --no-rdoc --version=3.0.1 rails

Confirm installation with the below command. This command will display the version of rails installed on the system.

rails -v

Install RubyGems:
Installation of rvm automatically installs RubyGems
Give the below command to confirm this. This command would display the path in which the gem has been installed.

which gem

Update the system to the latest version with the below command:

gem update –system