Archive for December, 2009

RubyInstaller RC1 is out

There’s finally an easy way to install Rails with Ruby 1.9 using the recently released RubyInstaller (Release Candidate). RubyInstaller uses MinGW (Windows port of GCC compiler) to create binaries and provides better compatibility with the Windows environment. You can get more info and download it here.

This marks the end of Ruby’s OneClickInstaller that majority of people used to install Ruby.

Main things to note (from the official FAQ):

Q: Why can’t I simply download the latest MRI binary distributions for Windows from

A: The binary packages for Windows found on lack several key components, such as OpenSSL, Zlib and Readline, which results in a broken experience for users trying install these binary distributions out-of-the-box. To compound the issue, other extensions are built-in, but lack essential bindings such as Tk and gdbm. Locating and installing the correct versions of these missing components can be tricky; RubyInstaller seeks to alleviate these difficulties and make the installation process dead-simple by providing everything you need to get started in one straightforward installation package.

Q: If I install both versions, is there some graceful way of selecting which version is active at a given time (e.g., which ruby.exe is invoked, which irb.bat is called, etc.)?

A: Pik is excellent for this coordination—check it out


I haven’t yet installed it myself, but will definitely try it out. If you already have experience with the RubyInstaller – drop me a comment!

December 26, 2009 at 10:42 am Leave a comment

Commenting out chunks of view

It’s not as easy to comment out big chunks of Rails erb (view) code. Here’s the one that seems to work fine for people:

<% if false %><!-- comment start -->
<% ... some code ... %>
some html
<% ... more code ... %>
<!-- comment end --><% end %>

If you simply try using HTML comments, the code will still execute.

Finally, if you want to simply comment out a single ERB statement, put # just after <%, like this:

<%# some ror code here %>
<%#= or some talking ror code here %>

December 14, 2009 at 3:33 pm Leave a comment

Upgrading the script/console in Windows VISTA environment

If you haven’t been actively using script/console – you should start it now. It allows you to test things out in the environment of your project with a direct feedback. So, if you want to check those quirky database queries or how a new plugin works, run the script/console and start coding.

However, as seemingly everything in Ruby Rails, the console is a bit buggy in Windows. This calls for a console upgrade. At the same time we will include some further improvements as well to make your console the best firend.

1. Buggy cursor. Try it for yourself – type a very long line of code and try to move cursor – chances are it will get screwed up at some point in time – the cursor will jump, etc. The problems is with a module called readline. It’s a part of Ruby and was not updated from 2005, and nobody bothered to fix it for Windows. So, the only option is simply to disable it in your irb.bat file by adding the –noreadline switch like that:

IF NOT "%~f0" == "~f0" GOTO :WinNT
@"C:\Ruby\bin\ruby.exe" "C:\Ruby\bin\irb" --noreadline %1 %2 %3 %4 %5 %6 %7 %8 %9
"%~d0%~p0ruby.exe" "%~d0%~p0%~n0" --noreadline %*

Now, you won’t be able to use auto complete features of your console (tab), but the cursor works and the auto complete was not that useful for me.

While there, create a shortcut file called rc.bat in the ruby/bin directory, and insert there:

ruby script/console

2. Install HIRB and Wirble. HIRB is a nifty gem returns ActiveRecord objects in an SQL like table, which makes it much more readable:

irb>> Tag.last
  | id  | created_at              | description | name          | namespace | predicate | value |
  | 907 | 2009-03-06 21:10:41 UTC |             | gem:tags=yaml | gem       | tags      | yaml  |
  1 row in set

To install hirb run: gem install hirb

Wirble does some colorizing stuff, although I have to say the default color scheme looks awful. To install wirble run: gem install wirble

You will also need to install win32console to get ANSI color support: gem install win32console

3. Now, automatically add HIRB (tabular query results), Wirble (color) and showing resulting SQL queries to the console.

In C:\Users\[your user name] create a file called “.irbrc” and paste these instructions there:

# script/console configuration
# v 1.0, 2009 Nov

  require 'rubygems'

# load hirb
  require 'hirb'
  extend Hirb::Console

# load Win32Console
  require 'Win32/Console/ANSI'

# start wirble (with color)
  require 'wirble'

# show SQL statements on in console mode
  if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
    require 'logger'

So, now you have a modified console that works much better than the out of the box solution.

December 10, 2009 at 1:03 am 4 comments

vote_fu to work in Heroku (Postgres)

In order to make vote_fu work in Heroku’s PostgreSQL you need to make a slight modification in the tally method, to follow a classical SQL notation:

Add this method in acts_as_voteable.rb just after options_for_tally method:

	def column_names_for_tally { |column| "#{table_name}.#{column}" }.join(", ")

In options_for_tally method find line that starts with “group_by =” and replace it with:

group_by  = "#{Vote.table_name}.voteable_id, #{column_names_for_tally}"

That’s it. Should work now.

December 6, 2009 at 3:19 pm Leave a comment

Rails 2.3.5

Rails 2.3.5 is out. It provides a better Ruby 1.9 compatibility, you will now not need to escape everything with h() if you install a RailsXss plugin – I think this is great, and now the Nokogiri XML parser can be used.

By the way, to see how powerful Nokogiri is for parsing HTML, watch the episode on screenscraping by Ryan Bates.

December 5, 2009 at 7:35 pm Leave a comment

Hosting your Rails app – first look at Heroku

Introduction: You should really get professional help

I am building an app and needed to show an unfinished version to some guy. As I am building on a local (windows!) box, I decided that it’s time to invest some time in finding out what options there are to host a Rails app.

My first choice was my old PHP shared hosting for which I pay for the last 7 years and it just lies there. So, my hosting company said they support Rails, so after some fiddling with cpanel I’ve uploaded my RoR files… One hour later I found out from the customer service that they do not support any deployment tools. Further, to install any gems I need to send them a request via email… Given that Rails app can easily have 10-15 gems, I thought this was a show-stopper. This is what I think you will find with non-specialized Rails shared hosting environments – they will be too difficult for you to work with, so just don’t bother.

Ok, I’ve decided to shop around and one thing that I always wanted to try was Heroku. It’s a “different type” of hosting, but come to think of it, quite similar to how I already work with Rails (read: loads of console commands). The ease of deployment will very much depend on your application – how complex it is, what sort of gems you use, etc.

Heroku prides itself on making the deployment effortless. And for some projects it might as well be true. For me it was not a 5 minute job, I took 2 full days to transfer the project, because there were several things that needed to be changed in the code, and I also had to learn several things – like Git and S3, that were on my to do list anyway (so that’s all right 😉 ). But, now that I know the limitations of Heroku, I would be able to build my future apps with “Heroku compatibility” (more on this in Gotcha section) built-in.

Heroku – getting started

1 – Sign up. The best thing about Heroku is that you can start by using their free hosting plan. Just register on their site.
2 – Git and Rails installed. I assume you have Rails installed locally, and you should also have Git installed for Heroku to work.
3 – Run “gem install heroku” to install the heroku gem. It will add a “heroku” command tool to your command line that will be the way for you to work with your hosting – there will be no ftp and no web control panel (the control panel is still used to switch addons on and off, but majority of activities are done from the command line). After installing the gem, try running “heroku” – it should show you a list of available commands.
4 – Heroku actually has a good Quick start guide that you could follow easily. There are some tricks for a Windows user, so I will cover them here:

4.1. Using Git. First, you will need to initialize the Git repository for your code. If you never used Git for anything other than downloading plugins, you need to start learning to use it as your VCS of choice. If you are already using some VCS for Rails, you will need to move to Git in order to use Heroku. As I didn’t use a VCS, it was easy for me.

4.2. Switching Git on:

* Go to the root directory of your rails app (the one you want to host).
* Heroku has a 50mb limit on the size of your Rails project for a free hosting plan. So, before running any commands, remove any obvious garbage from the directory (old copies, big archives, graphic and video files you don’t need).
* If there are some things that you’d rather leave in the project directory but want Git to ignore, create a .gitignore file in the root of your project. Some useful info on .gitignore is here.
* Ok, if you are all set, run the following commands:

git init
git add .
git commit -m "first version"

The first line initializes a new repository. Second adds all files in the current directory to the repository. The last commits a “release”, and -m “text” provides a comment. Now is the time to invest into learning the basics of Git, as you will be using it a lot. A good introduction on Git is here.

4.3. Creating SSH keys. Now you need to create a public key to allow Git to work using SSH with the Heroku host. Here the Heroku documentation is sparse, so hopefully you will find these instructions helpful:

* Go to the Git\bin directory (for me it was in c:\Program Files) and run “bash” command. It will show you a unix-like prompt.
* Run this command in bash, providing your Heroku email after -C switch:

ssh-keygen -t rsa -C ""

* When prompted to provide a name for the key, type a name “id_rsa” and hit enter.
* When asked for a password you can leave it blank or set one up – it’s up to you. If you set a password, you will be prompted to type it every time you upload to Heroku
* Now, in your Git/bin directory find the just created 2 keys – public and private.
* Move those files to your c:\Users\[your username]\.ssh directory
* Finally, upload the public file (by default with .pub extension) to Heroku by using this command:

heroku keys:add

If it doesn’t find it, you can explicitly set a full path to your key, like this:

heroku keys:add c:\key\


If you did everything correctly, it will just work. If Git won’t find a local private key that matches the public key you uploaded, you will get an error like this: Permission denied (publickey).

Finally, I first tried to use puttygen to generate the keys but did not succeed.

5 – Creating your Heroku app. Now from your Rails project’s root directory run

heroku create

It creates the space for your application, provides a temp url (you can change it later) for you app, and so on. If it asks you for login / pwd, enter the ones you registered on Heroku with.
6 – Uploading app to Heroku. From the same project directory simply run

git push heroku master

It might ask you for a public key password from step 4.3. But other than that you’re all set. In theory after it successfully uploads, you can run “heroku open” to open your app in browser, but it doesn’t work for me in Windows. You can open it manually in your browser or from the Heroku web console.

7 – Heroku Gotchas – what you should know!

7.1. – Database access

After you upload your scripts, you will want to create the database based on your production database. There are two options. You can run “heroku rake db:migrate” to walk through each migration file. However, if during development you mishandled at least one migration file, it will be tough to fix. A better way is to just upload the schema – “heroku rake db:schema:load”.

Heroku does not allow you to work with the database (or files for that matter) directly, so it’s good if your development environment follows exactly that of Heroku. And Heroku runs on a somewhat less popular PostGres DB, so if you have some more advanced SQL in your code, you’re in for some fun trying to rectify the SQL errors that are working fine on MySQL and SQlite. For me, two plugins stopped working due to a PGError SQL error, and it took a couple of hours to fix.

Heroku also provides a way to push (and pull) data to the production database, but it didn’t really work for me, giving some cryptic error, so I raised a ticket with Heroku CS – let’s see how it goes.

7.2. – Logs and console

Both logs and console are accessible via heroku command. Run “heroku logs” to get the latest extract from logs and “heroku console” to load the console for debugging / testing purposes. These two commands will be your best friends debugging the hosted app.

There is another add-on that you can switch on, called Exceptional, that send you errors via email. But for me it worked with a huge 1-2 hour delay, so it’s not really useful.

7.3. – Installing gems

Heroku takes your vendor/plugins directory just fine, but the gems need some additional setting up to do. You need to list all gems your application requires in “.gems” text file in the root of your project. You can just list the names of gems or set a finer control, listing the version numbers and repositories. More info here.

7.4. – Your file space is read-only(!)

So, this might come as the biggest surprise to some of you, but apart from the scripts that you upload via Git you cannot write anything to disk. You need to host your uploads / userpics / videos, etc. elesewhere. The best alternative probably being S3. Of course, this will mean changing all your scripts to work with S3. Heroku even explains how for the most popular plugins – attachment-fu and paperclip.

7.5. – No mail / SMTP gateway provided
Heroku does not provide you with an SMTP gateway, you will need to find one yourself and modify the required config files accordingly.

7.6. – Configuration made differently
Heroku suggest you to provide it with the configuration variables using their own methods. More info here.

The verdict

Heroku seems to be a great way of deploying your Rails app. If you set it up according to the rules, deploying will be a breeze – a single line of code will take care of everything(!). If you haven’t been developing with the “cloud” architecture in mind, you’re in for some rewrites, but at least you do it only once.

One thing to note is that Heroku allows collaboration out of the box – this is the power of Git-based hosting for you.

Some things missing for me is the db console, and the db:push doesn’t work at the moment, so it feels like the debug is not fully possible.

I am also not sure about the performance of the application – there are several things added to the stack to allow for the magic. If you need some non-standard things, you most probably will want to have a hosting platform that’s open for you to do any hacks you need.

Still, as a “development box in the cloud” this is great, I recommend you to try it out for your hosting needs!

December 5, 2009 at 7:07 pm 11 comments

Recent posts

Starting to learn Rails?


Get Kindle - the best e-book reader, that I personally use, and the only one that you can read on the beach - very useful: Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)