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.
Update: You’ll need to install the following:
Ubuntu 10.10 -
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:
$ 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 --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 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:
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:
$ rails -v
Creating & running a test application
My recommendation is to setup a new directory for all your rails projects:
$ cd $HOME/proj/rails
Now create a new rails app called “testapp”:
And start the server… it should fail because sqlite3-ruby is not installed:
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”.
And now we can start the server!
[2010-10-03 02:35:11] INFO WEBrick::HTTPServer#start: pid=50672 port=3000
Git will be really important later, don’t skip this step. For starters, run inside your rail project:
$ 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.
$ rake db:migrate
Now go to http://localhost:3000/products/ and try it. Don’t forget to commit:
$ 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:
$ heroku create
Uploading ssh public key $HOME/.ssh/id_dsa.pub
Creating deep-planet-58.... done
Created http://deep-planet-58.heroku.com/ | email@example.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:
$ heroku db:push
Now check http://deep-planet-58.heroku.com/products/. For removing the app in heroku:
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.