Thursday, August 30, 2012

Creating a new Ruby on Rails Project

Though creation of a new project has been mentioned in our previous post on "Introduction to Ruby on Rails", I am taking out the exact part of creating a new project into a blogpost as it would be easy for you to bookmark it.


Creation of new Rails project - Project name: first_app

#creates a rails app with all required folders
rails new first_app

#enter the directory of our app
cd first_app



Edit the Gemfile  specifying the exact versions of the gems required

source 'https://rubygems.org'

gem 'rails', '3.2.8'

group :development do
  gem 'sqlite3', '1.3.5'
end


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.4'
  gem 'coffee-rails', '3.2.2'

  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.2'


#Install the gems specified
bundle install 

#start the server
rails server

Open the webpage http://localhost:3000

Wednesday, August 22, 2012

Deploying your Rails App


#We use Heroku to deploy our rails application - Install heroku gem
gem install 'heroku'

heroku add:keys

#The --stack cedar argument arranges to use the latest and greatest version of Heroku, called the Celadon Cedar Stack. It creates a subdomain for us
heroku create --stack cedar

#Just push/deploy the code using the following git push command as heorku works seamlessly with git
git push heroku master

#That's it, our application is deployed, you can view it using this command.
heroku open

Version Control of your Rails Application using Git

Creating and Configuring Git Repo

#Set your name for identifying the commits
git config  --global user.name ""

#Set your email for correspondence during collaboration
git config  --global user.email xyz@gmail.com

#creating a shortcut "co" for checkout
git config --global alias.co checkout

#while we are inside our app directory, initializing a git repository
git init

#Adding all files in the current directory(.)
git add .

#Checking the status of the files under git repository. Since it's not committed yet, All of them woudl be visible
git status

#Committing the code with a message "Initial Commit" specified with -m flag
git commit -m "Initial commit"

#Now checking github status will give empty as everything is committed
git status

#Check the git log
git log

#Once the code has been commited, we can push it to Github for backup or collaboration
git remote add origin https://github.com/vamshi4001/first_app.git

#Push the code to remote origin added above as master branch
git push -u origin master


Branch, Merge, Delete and Push


#if you wanna edit something without affecting the original code, you create a branch
#This can be done using the following checkout "co" command with a flag -b and name of the branch
git co -b modify-README

#Check the list of branches with following command. It also shows which branch are we currently using with * before it
git branch

#Changing the extension of rdoc to md(Markdown Markup language), which github can understand and format neatly.
git mv README.rdoc README.md

#Check ths status of the git repo now. We have one rename and one modification
git status

#We need not add the renamed file now. We have a flag -a which will take into account all modified including the renamed ones.(Not newly created)
git commit -a -m "Improved the README file"

#Now we have committed to a branch created. We'll switch back to our master with following command.
git co master

#Now merge the changes in the branch "modify-README"
git merge modify-README

#check the branches, we'll be currently on master(indicated by a * beside master)
git branch

#Since we are done with modifications and merged, you can abandon it or delete it using a -b flag as follows
git branch -d modify-README

#Now push the modified and committed code to github
git push


Introduction to Ruby on Rails

This blogpost is kind of summary for most of the tutorials out there on internet. Once you've gone through them and if you would like to note important steps - just bookmark this post.

Ruby on Rails Setup

We need rvm, ruby, rails and creation of a gemset

#Install rvm
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

#check versions
rvm -v

#Gives the list of available ruby versions
rvm list known

#Install ruby version 1.9.3
rvm install 1.9.3

#Check version of ruby
ruby -v

#Create a Gemset rails3tutorial2ndEd and use it as default
rvm use 1.9.3@rails3tutorial2ndEd --create --default


Now Install rails for this particular gemset and ruby version


sudo rvm use 1.9.3@rails3tutorial2ndEd do gem install rails

Now all required gems are installed.
Also install git along with this. So now we have Ruby, RVM, Rails, Git
Check GEMS with "gem list"


Now create a rails projects

#creates a rails app with all required foldersrails new first_app

#enter the directory of our app
cd first_app



Edit the Gemfile  specifying the exact versions of the gems required

source 'https://rubygems.org'

gem 'rails', '3.2.8'

group :development do
  gem 'sqlite3', '1.3.5'
end


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.4'
  gem 'coffee-rails', '3.2.2'

  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.2'


#Install the gems specified
bundle install

#start the server
rails server

Open the webpage http://localhost:3000