I really, really liked rails 3. So much, I tried really hard to use it in another project… and failed.
Well, among other things, most people use Microsoft Windows out there. And they find extremely difficult unix-style installations. For me, it’s natural the solution of installing libssl-dev and rebuilding ruby when “gem install” fails because an error ocurrs in net/https. But normal people don’t.
So, if you are running Windows this howto is for you.
Installing rails 3
Because Windows doesn’t have a system wide ruby installation (like Mac OS or Linux), it doesn’t make sense to use something atop like RVM. We just need to install ruby 1.9.2 and then go up.
Just follow the wizard, but in the 3rd screen select “Add Ruby to PATH”.
Ruby will be installed inside C:\Ruby192. To check everything is ok, open a console and run “ruby -v”, you should see “ruby 1.9.2p0 (2010-08-18) [i386-mingw32]“.
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
C:\> gem install rails
C:\> rails -v
Creating & running a test application
Now create a new application
And start the server. It should fail (like in unix) because sqlite3-ruby is not installed:
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”.
If you try to run the server again, you’ll get an error:
Go to http://www.sqlite.org/download.html and below “Precompiled Binaries For Windows” download both sqlite-3*.zip and sqlitedll-3*.zip the “Precompiled Binaries For Windows” (I’m using sqlite-3_7_3.zip and sqlitedll-3_7_3.zip).
Extract all files (sqlite3.exe, sqlite3.dll and sqlite3.def) to the C:\Ruby192\bin folder. Now finally the server starts.
C:\testapp> rails s
[2010-11-19] 06:48:58] INFO WEBrick::HTTPSERVER#start: pid=1836 port=3000
On the 4th screen, check everything:
On the 6th screen, select “Run Git from the Windows Command Prompt”:
On the 7th screen, select “Checkout as-is, commit Unix-style line endings”:
Once msysgit is installed, open a new cmd console and configure your name and email
C:\testapp> git config --global user.email "email@example.com"
You can get help about any git command by typing the command and –help:
Git will be really important later, don’t skip this step. Do your first commit:
C:\testapp> git add .
C:\testapp> git commit -m "project started"
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.
C:\testapp> rake db:migrate
Now go to http://localhost:3000/products/ and try it. Don’t forget to commit:
C:\testapp> 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, upload your ssh keys to your home folder (something like C:\Users\Aldrin\.ssh\). If you don’t have a key, press the right button on the testapp folder and select “GIT Bash Here”.
Create your key with the following command:
Now, register in http://api.heroku.com/signup … After creating your account and uploading your ssh keys, install heroku and create a new app:
C:\testapp> heroku create
Uploading ssh public key C:\Users\test/.ssh/id_dsa.pub
Creating electric-sunset-57.... done
Created http://electric-sunset-57.heroku.com/ | firstname.lastname@example.org:electric-sunset-57.git
Git remote heroku added
C:\testapp> 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://electric-sunset-57.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:
C:\testapp> heroku db:push
If still fails, you can check heroku logs:
I’ve found out that heroku didn’t find sqlite3 (which is not be available in their production environment), so I’ve changed the Gemfile to this:
gem 'sqlite3-ruby', :require => 'sqlite3'
Commit your changes and push them to heroku.
C:\testapp> git commit -m 'bugfix: sqlite3 only in dev,test'
C:\testapp> git push heroku master
Now check http://deep-planet-58.heroku.com/products/. For removing the app in heroku:
Bonus: TextMate like editor
Many tutorials you’ll see use TextMate for programming in rails, which is only available in Mac OS X. Try the following alternatives:
First, download java for windows. Now, install redcar (this will take a while…):
C:\testapp> redcar install
C:\testapp> redcar .
You may run redcar directly from C:\Ruby192\bin\redcar.bat .
That’s all for now.