Rails Commands

Summary of various ruby commands

.erb: embedded ruby, the primary template system for including dynamic content in web pages.


$ rails generate integration_test static_pages

Undoing things

$ rails generate controller StaticPages home help
$ rails destroy  controller StaticPages home help

$ rails generate model Foo bar:string baz:integer
$ rails destroy  model Foo

$ rake db:migrate
$ rake db:rollback
$ rake db:migrate VERSION=0

Running tests

$ bundle exec rspec spec/requests/static_pages_spec.rb

Embedded Ruby template

<% provide(:title, 'About Us') %>
<!DOCTYPE html>
    <title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>

Unix commands

mv: rename

$ mv app/views/layouts/application.html.erb foobar

Git commands

Commit, merge, and push

$ git add .
$ git commit -m "Finish static pages"
$ git checkout master
$ git merge static-pages
$ git push

RVM and bundler integration

$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ cd ~/rails_projects/sample_app
$ bundle install --without production --binstubs=./bundler_stubs

rails generate basics

create controller with help and home routes

$ rails generate controller StaticPages home help --no-test-framework

the option no-test-framework suppresses the default rspec test (or test::unit).

if you have created them by mistake, you can destroy them.

$ rails destroy controller StaticPages home help --no-test-framework

Creating a model is the same.

$ rails generate model Foo bar:string baz:integer
$ rails destroy model Foo


Could not find a javascript runtime.
Install node.js.

Setting up ruby on rails on ubuntu

First of all, I am following on-line version of rails tutorial (http://ruby.railstutorial.org/ruby-on-rails-tutorial-book?version=3.2#top). Michael recommends sublime text 2 as editor. I use textmate on my mac, but I wouldn’t have the luxury of mac at work, so I am giving a try.

open a terminal here

Since I comes from windows background, I don’t like navigating in terminal. This util will install a menu on nautilus file browser

$ sudo apt-get install nautilus-open-terminal

sublime text

You can download sublime text 2. I did and it is now in download folder. Ah, I don’t know how to install application ubuntu. Do I have to run setup file like windows or drag and drop to application folder like mac? Sublime forum says I can create an “apps” folder in /home/{user}/ and put it there. Luckily, the text seems to be a stand-alone files, so I copied the directory to /home/andrew/apps/.

install Git

The free chapter of Pro Git is very helpful. I simply did

$ sudo apt-get install git-core

Install Ruby

I did $ruby -v, and it has ruby 1.9.1. It should be all right, but I want to use the latest available. Michael recommends installing ruby by using RVM, so I will go for it.

I will use “aptitude”, following the instruction. To find out if you have “aptitude”, you can do

$ sudo aptitude

If you get an error then, you need to install it by doing the below.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install aptitude

Let’s install git core and curl

$ sudo aptitude install build-essential git-core curl

Then install RVM. This helps you manage multiple versions of ruby.

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

This is a kind of weird command to me, a ubuntu beginner, but it seems to work. Once RVM is installed, you need to load it to terminal. By adding the following command, you can make it be loaded automatically each time you start bash shell.

$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc

Check if it is successfully installed. You may have to close your current terminal session and open again to load RVM. type

$ rvm notes

You should see the note if it is installed.

Now install several helpful ruby packages.

$ sudo aptitude install build-essential openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev zlib libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison

Finally, you are ready to install ruby. So far, all packages were for ruby, not ruby.

$ rvm get head && rvm reload
$ rvm install 1.9.3

Install Rails

use gem to install rails. I had to use “sudo” due to permission issue on my ubuntu.

$ gem install rails -v 3.2.3
$ sudo apt-get install libxslt-dev libxml2-dev libsqlite3-dev

Check the version by doing $ rails -v.

After I install rails in this way, I get an error saying “invalid gemspec invalid date format” It seems Ubuntu prefers installing rails through apt-get install gem

$ apt-get install gem

Please let me know if anything does not work in this post.

Afterward …


It is quite frustrating that you go through quite a few errors. It makes me miss windows’ simple msi installer!

“Could not find railties” error. For some reason, railties are not installed.

$ sudo gem install railties

“Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://bb-m.rubygems.org/gems/rack-1.4.1.gem)
An error occured while installing rack (1.4.1), and Bundler cannot continue.
Make sure that `gem install rack -v ‘1.4.1’` succeeds before bundling.”

This one is self-explanatory, I think. Just install rack.

$ sudo gem install rack -v 1.4.1

ruby version problem.
I kept losing RVM whenever I start a new terminal session. Put this settings to your bash config

$ gedit .bashrc

Then put this at the end of the file.
[[ -s “$HOME/.rvm/scripts/rvm” ]] && . “$HOME/.rvm/scripts/rvm” # This loads RVM into a shell session.

Then RVM will be loaded automatically, and you will have the same version of ruby each time.

not function. ‘rvm use …’ will not work
If you run rvm use on ubuntu terminal, you get this message. It is because your bash shell is not login shell by default. Open the terminal settings and integrate rvm with gnome terminal.