How to start programming in rails 3

Although I have no news about the perfect web application technology quest, I have been experimenting with a lot of frameworks (like Google Web Toolkit and Cappuccino) during the last 6 months… just didn’t got the time to post about my findings or even start the sample application.

Last week I started a REAL project using Ruby on Rails 3. The first two hours was quite exciting, but after a while I realized that all examples/docs/podcasts/guides were made with the 2.x versions in mind. Everything requires several adjustments for the new 3.0 rails version.

So, I’m beginning to document step-by-step instructions on how to start programming in the shiny new rails 3.

Prerequisites

Update: You’ll need to install the following:

Ubuntu 10.10 -

$ sudo apt-get install curl zlib1g-dev libsqlite3-dev libssl-dev

Installing Rails 3

Thanks to  RVM, this is quite easy: RVM will download, compile and install everything inside a sandbox directory ($HOME/.rvm/), no need to touch your system libraries or becoming root. You can install many versions of ruby, or go back to your system wide installation. Easy and reliable.

First, run the RVM install script and then include RVM in your .bashrc or .profile:

$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> $HOME/.bashrc

Quit/restart your bash or terminal session. Now install ruby 1.9.2

$ rvm install 1.9.2
$ rvm --default 1.9.2

Note: If you are in Mac OS X, ruby is not compiled with iconv support (crucial for i18n!)… run this to install iconv inside the sandbox and reinstall ruby:

$ rvm package install iconv
$ rvm remove 1.9.2
$ rvm install 1.9.2 -C --with-iconv-dir=$HOME/.rvm/usr
$ rvm --default 1.9.2

You can switch between your system wide ruby or the new one inside the sandbox, just run both and keep with the 1.9.2 version:

$ rvm --default system && ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
$ rvm --default 1.9.2 && ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]

Install rails 3 with gem (it may take a while) and check:

$ gem install rails
$ rails -v
Rails 3.0.0

Creating & running a test application

My recommendation is to setup a new directory for all your rails projects:

$ mkdir -p $HOME/proj/rails/
$ cd $HOME/proj/rails

Now create a new rails app called “testapp”:

$ rails new testapp && cd testapp

And start the server… it should fail because sqlite3-ruby is not installed:

$ rails s
Could not find gem 'sqlite3-ruby (&>= 0, runtime)' in any of the gem sources.
Try running `bundle install`.

Instead of running “gem install foo bar”, rails 3 comes with bundle: when you get a new project, run bundle to check dependencies and install missing gems. You can specify your own dependencies in the “Gemfile”.

$ bundle

And now we can start the server!

$ rails s
[2010-10-03 02:35:11] INFO  WEBrick::HTTPServer#start: pid=50672 port=3000

Check http://localhost:3000/.

Setup git

If you don’t know git, you should learn the basics right now ;-)

Git will be really important later, don’t skip this step. For starters, run inside your rail project:

$ git init
$ git add .
$ git commit -m "first import!"

Create a new CRUD model, controller, view

Rails is based on MVC and “scaffolding” is what they call to create a simple controller, model and view for a very basic and useful CRUD template.

$ rails g scaffold product title:string desc:string price:integer
$ rake db:migrate

Now go to http://localhost:3000/products/ and try it. Don’t forget to commit:

$ git add .
$ git commit -m "created product mantainer"

Upload your app to heroku

This is really, really cool, you can upload and update your app for free in heroku using nothing but the command line!

First, register in http://api.heroku.com/signup … After creating your account, install heroku and create a new app:

$ gem install heroku
$ heroku create
Uploading ssh public key $HOME/.ssh/id_dsa.pub
Creating deep-planet-58.... done
Created http://deep-planet-58.heroku.com/ | git@heroku.com:deep-planet-58.git
Git remote heroku added
$ git push heroku master

The last line is all the magic: whenever you push to the heroku repository, your app is redeployed… deployment has never been so easy!

Go to your application url (like in http://deep-planet-58.heroku.com/products/). It should fail because we need to setup a database in heroku servers… You can upload your current development sqlite3 database with:

$ gem install taps
$ heroku db:push

Now check http://deep-planet-58.heroku.com/products/. For removing the app in heroku:

$ heroku destroy --app deep-planet-58

Important things to remember

  • Don’t run any command as root because everything is installed inside $HOME/.rvm/
  • I repeat: never run sudo!
  • Declare dependencies in the Gemfile and run “bundle” to install required gems. Heroku will do the same, so get used to it
  • Setup git from the start and use it constantly
  • You can deploy your app in heroku, don’t forget to push your database

That’s all for now.

0 Responses to “How to start programming in rails 3”


  • No Comments

Leave a Reply