Ruby on Rails

From Tmplab

Jump to: navigation, search

This is valid for Rails 2.2.2 up to 2.3.3, the versions i'm using currently. May be valid for other versions.


Fast commands

  • Help yourself, use BORT for application baseline


ruby script/generate scaffold Project name:string description:text
ruby script/generate migration AddOwnerToProject
rake db:migrate
rake db:schema:dump


rake routes



  1. Install or
  2. Look in log/development.log
  3. Add "debugger" somewhere in the controller
  4. Relaunch the server using "ruby script/server --debugger"
    1. now commands can be typed like "list", "up", "irb" (for an Interactive Ruby session) or "cont"
    2. Remember the name of the top level object is "self" so printing something like "p self" can be useful

rdebug / ruby-debug

gem install ruby-debug

Debugging Rake tasks

rdebug -n /opt/local/bin/rake db:migrate
rdebug -n /opt/local/bin/rake rake ENVVAR=VALUE db:migrate

with this magical line of code:

require 'ruby-debug'; debugger;


Usually with:

ruby script/server


rake dbconsole
ruby script/dbconsole


rake console
ruby script/console





gem install <gemname>

if it breaks, try to do:

gem install

if it still breaks, try to do:

require 'rubygems'
gem 'myproblemgem', '1.0.0'

then look at the problem.


for example this session shows that the "echoe" gem is not installed. When running script/server:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
Missing these required gems:
  capistrano-ext  = 1.2.1

You're running:
  ruby at /usr/local/bin/ruby
  rubygems 1.2.0 at /usr/local/lib/ruby/gems/1.8

Run `rake gems:install` to install the missing gems.

well... indeed Captistrano-ext IS installed:

$ ls -ald /usr/local/lib/ruby/gems/1.8/gems/cap*
drwxr-xr-x   12 root  wheel  408 Jan 20 12:11 /usr/local/lib/ruby/gems/1.8/gems/capistrano-2.5.3
drwxr-xr-x    9 root  wheel  306 Jan 20 13:00 /usr/local/lib/ruby/gems/1.8/gems/capistrano-ext-1.2.1

but running the code with irb gives more details:

$ irb
>> require 'rubygems'
=> false
>> gem 'capistrano-ext', '1.2.1'
Gem::LoadError: Could not find RubyGem echoe (>= 0)

        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:578:in `report_activate_error'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:134:in `activate'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:158:in `activate'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `each'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `activate'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:158:in `activate'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `each'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `activate'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:158:in `activate'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `each'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:157:in `activate'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:49:in `gem'
        from (irb):2

So a simple:

# gem install echoe
Successfully installed echoe-3.0.2
1 gem installed
Installing ri documentation for echoe-3.0.2...
Installing RDoc documentation for echoe-3.0.2...

fixed the problem... well... you get the idea: there's a broken dependency in captistrano-ext for echoe....




Template applications


Initialize a new BORT instance

  • Edit
  • Type
Rake db:migrate

Modifying the Homepage

Edit file:


Modifying the Database

ruby script/generate migration AddProjectTable

or directly one of these:

ruby script/generate scaffold Project name:string description:text url:string
ruby script/generate scaffold Uploadbin hashid:string orig_filename:string stored_filename:string description:text url:string has_report:boolean owner_id:integer

Deploy with Capistrano

On the server host run:

ln -s
mkdir -p ~/

On local host, run

cap deploy:setup
cap deploy:migrations

Deploying on Dreamhost

ssh mkdir -p git/PROJECT.git
ssh "cd git/PROJECT.git/ && git --bare init"
git remote add origin ssh://
git push origin master
ssh "mv ; ln -s"
ssh "mkdir -p ~/"
ssh "touch ~/"
# GO TO dh panel to create MySQL db
cap deploy:setup
cap deploy:migrations
ssh gem install term-ansicolor
ssh gem install capistrano
ssh 'echo "export PATH=\$PATH:~/.gem/ruby/1.8/bin" >> .bashrc; echo "export PATH=\$PATH:~/.gem/ruby/1.8/bin" >> .bash_profile'
ssh 'cd `find|head -2|tail -1` && rake gems:install'
ssh "ls -ald ./ "
Personal tools