Install PHP 5.2 on Ubuntu Dapper

The repositories on Ubuntu Dapper don’t have php 5.2. To install php 5.2, I added the hardy repositories.

vi /etc/apt/sources.list

add the repositories
deb hardy main restricted
deb-src hardy main restricted

then run
sudo apt-get update
sudo apt-get install php5

This tried to install and upgrade a lot of packages including apache. I really needed php 5.2 and since the server is a development server, I accepted all the changes.

Sinatra: Suppress Newline in ERB

To suppress newline in ERB on Rails, you just add a dash on the closing tag -%>. In Sinatra, as of version 0.9.4, adding a dash doesn’t work. This has been fixed recently on the tilt gem version 0.3 which Sinatra uses for templates. You also need Sinatra newer than 0.9.4, which doesn’t exist yet so you need to get Sinatra from source.

You need to get tilt 0.3 from
sudo gem install --no-ri --no-rdoc tilt --source=

To get the latest Sinatra
cd myapp
git clone git://

and add it to the LOAD_PATH
$LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'

To set the dash as the ‘trim’ character, add
set :erb, :trim => '-'

RailsConf 2009

RailsConf 2009


One advantage of using rack is having access to rack middleware. rack-flash is a middleware which you can use for flash hash on your rack apps.

If you’re using sinatra, the syntax is similar to rails. Check out the rack-flash page here.

I added rack-flash to my sinatra app. Check out the git commit. Since rack-flash uses the session, don’t forget to add Rack::Session::Cookie.

Watch the video of Jon Crosby’s talk In a World of Middleware, Who Needs Monolithic Applications? at Mountain West Ruby Conference 2009 for more on rack middleware.

Sinatra, Datamapper, and Haml

I recently tried Sinatra and what’s a better way to learn a new framework than to create an app.

If you want to learn sinatra, the README will quickly show you what you can do with this framework.

I haven’t used Datamapper so I took this opportunity to learn it as well. You just need to require ‘dm-core’. That’s it. If you want to put your models on a separate file, you have to require that as well. If you’re using Rails, you’re probably used to models being available right away. I also used haml instead of erb.

The code is available at github. The Sinatra book has a list of Real World Applications. I looked at the seinfeld app as a basis for the structure.

There is more I want to learn in Sinatra. Since it uses Rack, I want to try using the different Rack Middleware available.


Paperclip recently made some changes making it more flexible.

Paperclip is a library for ActiveRecord used for file attachment. The most common use is for uploading images. It can optionally create thumbnails when you upload images. Paperclip requires ImageMagick for resizing images but it doesn’t require any Ruby libraries like RMagick or MiniMagick.

You can install paperclip as a gem or as a plugin. The paperclip gem needs right_aws, mocha, and shoulda.
gem install right_aws
gem install mocha
gem install thoughtbot-shoulda --source=
gem install thoughtbot-paperclip --source=

To install the plugin, run ./script/plugin install git://

Let’s say you have a User model and you want to add an avatar. On the users table, you need to add avatar_file_name.

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :login
      t.string :avatar_file_name

  def self.down
    drop_table :users

On user.rb, add has_attached_file

class User < ActiveRecord::Base
  has_attached_file :avatar

You just need to set avatar to an uploaded file and paperclip will handle the rest. On app/views/users/new.html.erb

  <% form_for :user, @user, :url => users_path, :html => { :multipart => true } do |form| %>
    <%= form.text_field :login %> <br />
    <%= form.file_field :avatar %> <br />
    <%= submit_tag 'Save' %>
  <% end %>

:multipart => true is needed when uploading files. Then on app/controllers/users_controller.rb

  def new
    @user =

  def create
    @user = User.create(params[:user])

@user.avatar.url will give you the url of the uploaded image. That’s all the code you need to use paperclip. There are more options for creating thumbnails, validating the model, and post-processing.

The uploaded files will be saved on RAILS_ROOT/public/system/:attachment/:id/:style/:basename.:extension. On my computer the file is saved on RAILS_ROOT/public/system/avatars/1/original/image.jpg.

As you can see, the default style is original. To add more ’styles’,

class User < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

When you upload the image, paperclip will save the original image, and resize the image to medium and thumb. On RAILS_ROOT/public/system/avatars/2, you will see the directories original, medium and thumb.

To get the medium and thumb images, you will use @user.avatar.url(:medium) and @user.avatar.url(:thumb).

On the users table, you can also add avatar_content_type (string), avatar_file_size (integer), and avatar_updated_at (datetime). These fields will be populated by paperclip automatically.

I will discuss validation and other post-processing options on my next post.

Rack-based Session Stores on Rails

Rails edge is now using Rack-based session stores. That means you can access the same session on any Rack applications. Read about it on the Rails blog.

Nothing changes though on how you use sessions on Rails. On your Rails app, you’ll have something like

session[:user] = some_user_id

Let’s see how we can access the same session on a Rack application. You can read about Rack here.

Rack handles session using Rack::Session::Cookie. Create a file

use Rack::Session::Cookie, :key => '_railssession_session', :domain => ''
run {|env| [200, {"Content-Type" => "text/html"}, "Rack session 'user' has a value of #{env['rack.session'][:user] || 'nil'}."]}

The key should be the same key that you used on config/environment.rb on your Rails app.

Check out the demo. Set session[:user] on the Rails app, then view the session on the Rack app.


Capistrano is used for “automating tasks via SSH on remote servers, like software installation, application deployment, configuration management, ad hoc server monitoring, and more.” It is written in ruby and is commonly used to deploy Rails applications. Capistrano can be used to deploy applications written on any language. It runs commands on remote servers via SSH so it’s not specific to any language.

I will show you how to use capistrano for deploying a Rails application. The goal is to deploy an application by running one command – “cap deploy”. This should update your code from Subversion, and restart your server.

I assume that you have a rails setup on your server. I use the nginx web server, mongrels and mongrel_cluster.

To install capistrano

gem install capistrano

To start using capistrano on your Rails application,

capify .

You should see two files – Capfile and config/deploy.rb. Capistrano looks for Capfile when you run any capistrano commands. The default Capfile loads config/deploy.rb. deploy.rb contains the settings for your application.

We will deploy the Rails app “Hello Labs”. This can be found on our repository. Let’s edit deploy.rb.

Set the user and runner that will run the commands on your server.

set :user, "crigor"
set :runner, "crigor"

Set the application name

set :application, "hellolabs"

Set the scm and repository

set :scm, :subversion
set :repository, ""

The default :scm is actually :subversion but I like to put it anyway.

Set the directory on the target server where you will deploy the application.

set :deploy_to, "/var/www/#{application}"

application is “hellolabs” which you set earlier. The deploy_to directory, which I’ll call DEPLOY_ROOT, is /var/www/hellolabs. You should create this directory on the target server. On Ubuntu, I set the group to www-data so mongrel has access to it.

Set the servers.

role :app, ''
role :web, ''
role :db, '', :primary => true

You can also use the IP address instead of the domain.

If nginx, mongrel, and mysql are all on the same server, put the same domain or IP address.

Now we will run our first capistrano task. Make sure /var/www/hellolabs exists on your server. On the RAILS_ROOT, run

cap deploy:setup

This will create the releases directory and shared directory under the hellolabs directory on the remote server. If you get a permission error, make sure the user you specified has access to /var/www/hellolabs.

Capistrano will ask for you server password. If you don’t want to enter your password every time you run a capistrano task, you can add

set :password, "mypassword"

Check if we have met the dependencies.

cap deploy:check

The output, if deploy:check is successful, looks like this.

Before we deploy the app, we define the deploy:restart task. By default capistrano runs /var/www/hellolabs/current/script/process/reaper. I use mongrel and mongrel_cluster so let’s add

namespace :deploy do
  desc "Restart the mongrels"
  task :restart do
    run "mongrel_rails cluster::restart -C #{shared_path}/config/mongrel_cluster.yml"

Create the /var/www/hellolabs/shared/config directory and add mongrel_cluster.yml. We also need to add database.yml. I don’t commit database.yml in my repository since it contains the database password.

We have to symlink mongrel_cluster.yml and database.yml every time we deploy.

after "deploy:update_code", "deploy:symlink_configs"
namespace :deploy do
  desc "Symlink the config files."
  task :symlink_configs do
    run "ln -nfs #{shared_path}/config/database.yml #{latest_release}/config/database.yml"
    run "ln -nfs #{shared_path}/config/mongrel_cluster.yml #{latest_release}/config/mongrel_cluster.yml"

We’re now ready to deploy our application.

cap deploy

If you see something similar to this, your app has been deployed.

The releases directory will contain directories with the timestamp as the name like 20081017125102 and 20081017132929. These directories will contain the rails application.

Below, you can see that current is a symlink to 20081017132929. /var/www/hellolabs/current is your RAILS_ROOT.


Check the URL of your app.

If you need a username and password to checkout your repostory, add

set :scm_username, "username"
set :scm_password, "password"

To get the list of capistrano tasks available, run cap -T.

For more information, check the capistrano website.

Here is the complete deploy.rb

set :user, "crigor"
set :runner, "crigor"

set :application, "hellolabs"
set :scm, :subversion
set :repository, ""
set :deploy_to, "/var/www/#{application}"

role :app, ''
role :web, ''
role :db, '', :primary => true

after "deploy:update_code", "deploy:symlink_configs"
namespace :deploy do
  desc "Symlink the config files."
  task :symlink_configs do
    run "ln -nfs #{shared_path}/config/database.yml #{latest_release}/config/database.yml"
    run "ln -nfs #{shared_path}/config/mongrel_cluster.yml #{latest_release}/config/mongrel_cluster.yml"

  desc "Restart the mongrels"
  task :restart do
    run "mongrel_rails cluster::restart -C #{shared_path}/config/mongrel_cluster.yml"

Railsconf 2008

I will be at Railsconf 2008 at Portland. I still haven’t finalized the sessions I’m attending but I’m sure it will be great.

RailsConf 2008

PNOI Judge

The first Philippine National Olympiad in Informatics (PNOI 2008) will be held on March 15, 2008 at Ateneo de Manila University, Loyola Heights, Quezon City.

PNOI is an individual programming contest for high school students. I’m a judge (and 4 others) at this contest.

Click here for the details.

Next Page →