Category: RoR

Ruby on Rails on Ubuntu on VirtualBox

I started exploring Ruby on Rails for a potential client project which never kicked off.  These are the steps I took to set up RoR on Ubuntu running on VirtualBox.

1.  Install VirtualBox 4.1.20

guide: https://www.virtualbox.org/wiki/Downloads

2.  Install Ubuntu 12.04

guide: http://www.psychocats.net/ubuntu/virtualbox

For resizing, copy/paste, share folders…
  a) “Install Guest Additions”, sometimes it doesn’t work and you have to go to /media/VBOXADDITIONS_… and run
      sh VBoxLinuxAdditions.run
  b) then enable clipboard in settings as Bidirectional
  c) then restart the instance.

For it’s own IP address
  a) go to settings->network and select Bridged Adapter
  b) if already running instance, reset network by running
    sudo /etc/init.d/networking restart

3.  Install Ruby on Rails using rvm

guide: https://www.digitalocean.com/community/articles/how-to-install-ruby-on-rails-on-ubuntu-12-04-lts-precise-pangolin-with-rvm

 4.  Run this to make sure rvm runs every time you open a new terminal

 echo ‘[[ -s “$HOME/.rvm/scripts/rvm” ]] && source “$HOME/.rvm/scripts/rvm”‘ >> ~/.bashrc

…or else this needs to be run when new terminal is opened
source ~/.rvm/scripts/rvm

 5.  as suggested on http://rubyonrails.org/download, create a skeleton site:

rails new ~/code/ror/HelloRoR
cd ~/code/ror/HelloRoR
rails server

…however this gives the following error:
Could not find a JavaScript runtime.

6.  To resolve: sudo apt-get install nodejs

7.  open a browser and go to http://localhost:3000

8.  ctrl-c to stop server (don’t just close term window or else process must be killed manually before restarting server)

9.  gedit-gmate as code editor

http://kibyegon.wordpress.com/2011/11/15/using-gedit-as-a-ruby-ide/
http://blog.sudobits.com/2012/03/11/gedit-plugins-for-ruby-on-rails/

10. mysql

sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
gem install mysql2

11.  Download mysql workbench for ubuntu 12.04

(later try to use workbench on host windows 7 machine and connect to mysql server on ubuntu, but it’s more involved)

From terminal, run
mysql-workbench

12. Create a RoR project which connects to MySQL

mkdir HelloRoRMySQL
cd HelloRoRMySQL
rails new HelloRoRMySQL -d mysql

13. useful generate scaffolding commands:

rails generate scaffold User userName:string fbId:bigint
  – since bigint is not accepted, go into db/migrate open the file and change
    t.bigint :fbId  to  t.column :fbId, :bigint
rake db:migrate
rails destroy scaffold User
rails g migration add_bio_to_user bio:text
  – this doesn’t modify views

14. to test

http://localhost:3000/users
http://localhost:3000/users.json