Improving the Usability of Your Ruby on Rails Applications

Ubuntu (Hardy): Install Apache 2 Server

Setting up Apache 2 server on Ubuntu Hardy is pretty easy.

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert


I choose apache2-mpm-prefork (single thread) over apache2-mpm-worker (multi-threaded requests). This is because mod_rails may work with apache2-mpm-worker, but is only currently tested to work with apache2-mpm-prefork.


Edit your apache configuration file:

$ vim /etc/apache2/apache2.conf

At the end of the file add the following:

ServerName your_server_name

And restart your server:

$ sudo apache2ctl graceful


If you get the following error when restarting Apache it means that you didn’t succeed in setting the ServerName:

apache2: Could not reliably determine the server's fully qualified domain name, using for ServerName


Start your prefered browser and open your server ip address (e.g., you should see something like this:

It Works!

Or use curl (command line is always better):

$ curl

You should see:

<html><body><h1>It works!</h1></body></html>

It Works!!!

Ruby: Mixin Template

This is the basic structure I use to create a mixin in Ruby:

# = Mixin Template
# == Usage
# ActionController::Base.send :include, MixinModuleName
module MixinModuleName
  def self.included(recipient)
    recipient.class_eval do
      include InstanceMethods
  module InstanceMethods
  module ClassMethods

Git: Creating a Remote Branch (and track it locally)

Required steps to create and track locally a remote branch with git:

# Create new remote branch
git push origin origin:refs/heads/new_branch_name
# Make sure everything is updated
git fetch origin
# Check your branch has been created
git branch -r
# Track a remote branch
git branch --track new_branch_name origin/new_branch_name
# Checkout remote branch
git checkout new_branch_name

Git: Undo commit

Sometimes, when the caffeine no longer works, you can make a mistake on a commit. Either you have second thoughts about your commit message, you just forgot to track new project files, or whatever the reasons are you can always undo your last commit (as long you don’t push it to origin repository):

$ git reset --soft HEAD^

Gem: Simple Navigation (Navigation menu builder for Ruby on Rails)

I’ve just released my new gem simple_navigation. This gem help’s you to create a navigation menu for your Ruby on Rails application.


Edit your config/environment.rb to setup simple_navigation gem:

config.gem "mexpolk-simple_navigation",
    :lib => "simple_navigation",
    :source => ""

And from the command line, install the plugin:

rake gems:install


To create your menus create a new file named config/initializers/simple_navigation.rb like this:

SimpleNavigation::Builder.config do |map|
  map.navigation :default do |navigation|

    # Root menu without child elements (menus) that points to /dashboard :home, :url => { :controller => "home", :action => "index"}

    # Root menu with child menus without anchor link :contacts do |contacts|

      # Child menu with many possible urls (or many controllers and actions) :list, :url => { :controller => "contacts", :action => "index" } do |contact_list|

        # This menu will marked as current when you're on the following
        # controllers and actions (including the controller and action
        # specified in the :url option):
        contact_list.connect :controller => "contacts" # ...current on any action from this controller
        contact_list.connect :controller => "people", :except => "new"
        contact_list.connect :controller => "companies", :except => "new"


      # Another submenu that points to /person/new :new_person, :url => { :controller => "people", :action => "new" }

      # Another submenu that points to /company/new :new_company, :url => { :controller => "companies", :action => "new" }


    # Another root menu with nested submenus :admin, :url => { :controller => "users", :action => "index" } do |admin| :users, :url => { :controller => "users", :action => "index" } do |users| :reports, :url => { :controller => "user_reports", :action => "index" } do |reports|
 :activity, :url => { :controller => "user_reports", :action => "activity" }
 :login_atempts, :url => { :controller => "user_reports", :action => "login_atempts" }
        end :new_user, :url => { :controller => "users", :action => "new" }


Finally, to render you newly created menu called :default, in your default layout (layout/application.erb):

<%= simple_navigation :default %>

Ready for Internationalization (i18n)

If you want to use internationalization in your menus, set the option :i18n => true like this:

SimpleNavigation::Builder.config do |map|
  map.navigation :default, :i18n => true do |navigation|

And add to your config/locales files (e.g. es-MX.yml) the following:

    default:                          # The name of your navigation menu
      home:                           # The name of your root menu
        title: "Inicio"               # The translated title for your root menu
            title: "Panel de Control" # The title for index action child menu
            title: "Nueva Página"     # The title for new action child menu

Sample Application

I have a sample application at GitHub so you can see it work. This application has the following configuration (config/initializers/simple_navigation.rb):

SimpleNavigation::Builder.config do |map|
  map.navigation :default, :i18n => true do |navigation| :home, "Wellcome", :url => { :controller => "home", :action => "index" } do |home| :settings, "Appliction Settings", :url => { :controller => "home", :action => "settings"}
    end :pages, :url => { :controller => "pages", :action => "index" } do |pages| :page_one, "One", :url => { :controller => "pages", :action => "one" } :page_two, "Two", :url => { :controller => "pages", :action => "two" }

When you render the navigation menu, it creates an unordered list like this:

<ul id="simple_navigation_default" class="simple_navigation" depth="0">
  <li id="simple_navigation_default_menus_home" class="menu" drop_down="true">
    <a href="/home">Wellcome</a>
    <ul id="simple_navigation_default_menus_home_menus" depth="1" style="display: none;">
      <li id="simple_navigation_default_menus_home_menus_settings" class="menu" drop_down="false">
        <a href="/home/settings">Appliction Settings</a>
  <li id="simple_navigation_default_menus_pages" class="menu current_child" drop_down="true">
    <a href="/pages">Pages</a>
    <ul id="simple_navigation_default_menus_pages_menus" depth="1" style="display: none;">
      <li id="simple_navigation_default_menus_pages_menus_page_one" class="menu" drop_down="false">
        <a href="/pages/one">One</a>
      <li id="simple_navigation_default_menus_pages_menus_page_two" class="menu current" drop_down="false">
        <a href="/pages/two">Two</a>

This result, with a little help of our friends CSS+Javascript will result in something like this:

Screenshot-Simple Navigation - Sample Application - Mozilla Firefox

Screenshot-Simple Navigation - Sample Application - Mozilla Firefox

Check the Simple Navigation Example yourself… Hope you enjoy it!

Ruby: Convert Number to Words (Numerical)

Recently I’ve published my new gem NumberToWords. This plugin/gem will override Ruby’s Numeric class adding a new method called to_words. For now, it only works for Spanish.

Sample usage:

require 'rubygems'
require 'number_to_words'
=> cinco mil seiscientos setenta y ocho”

Another common usage is for describing currency quantities:

number = 4567.90
=> 4567.9
number.to_words.capitalize << ' pesos ' << (number.to_s.split('.')[1] || 0).rjust(2,'0')
=> "Cuatro mil quinientos sesenta y siete pesos 09/100 M.N."

Happy Hacking!

GEdit: Snippets for Ruby on Rails

I’ve just created a new GitHub repo with GEdit snippets for Ruby on Rails. Any suggestions and comments are welcome. To install them simply copy the xml files to your ~/.gnome2/gedit/snippets directory.

GEdit Snippets Plugin

Ubuntu: Disable That Annoying Touchpad Click

In my opinion, one of the worse inventions since “PC”, is the annoying (and I’m been respectful) Touchpad. Not only it takes you out of your home row (keyboard), or the fact that you need like two passes for reaching corners. But because it really &*^#$%^*$ annoys me when typing accidentally make a click.

Lucky me… there’s a way to disable Touchpad-Click. Simply go to System > Preferences > Mouse, click on Touchpad tab, and uncheck: Ennable mouse clicks with touchpad.

So long Touchpad-click!!!

Git: Adding a Ruby on Rails Project to Git

Here’s how to add a recently created Ruby on Rails project to git:

1. Create your new project:

$ rails -d mysql project_name

2. Create some .gitignore empty files so you save the entire structure of your project (git doesn’t include empty folders):

$ touch db/.gitignore lib/.gitignore log/.gitignore tmp/.gitignore vendor/.gitignore

3. Create a new .gitignore file in the root directory of your project with the following content:


Note: If you are using sqlite add db/*.sqlite3 to .gitignore so you do not have the database into the repository (you can db:migrate it whenever you need it). Thanks for your reply Piku.

4. Initialize local repository:

$ git init

5. Add the project files and make que initial import:

$ git add .
$ git commit -a -m "* Initial import"

6. Finally if you have a gitosis server you can add it with the following commands:

$ git remote add origin git@YOUR_SERVER_NAME:project_name.git
$ git push origin master

That’s it, your project sources are now managed by git.

Happy hacking!


Get every new post delivered to your Inbox.