For my latest web project using Ruby on Rails, the idea of a mobile version arose.

Is it worth it? Well, how much more code would I have to write? 1,000 lines? Pages? Try 10. That’s right: 10.

Here’s the magic, first add the lines around the Mobile/lite version bit to config/routes.rb:

ActionController::Routing::Routes.draw do |map|
  # Default page is the dashboard
  map.connect '', :controller => "dashboard", :action => 'show'

  # Mobile/lite version
  map.connect ':mobile/', :controller => "dashboard", :action => 'show'
  map.connect ':mobile/:controller/:action/:id'

  # Install the default route as the lowest priority.
  map.connect ':controller/:action/:id'
end

Then edit app/controllers/application.rb:

require_dependency "login_system"

class ApplicationController < ActionController::Base
    include LoginSystem
      
    helper :debug
    helper :date_picker
    
    layout :determine_layout
    
    private
    
    def determine_layout
      if @params[:mobile]
        "mobile"
      else
        "application"
      end
    end
end

And finally, create app/views/layouts/mobile.rhtml

<%= yield %>

Surf on over to yourserver/yourapp/mobile/ And voila! You have a lite version of your site perfect for viewing on a smartphone or PDA. As a bonus, when you start using a mobile page, your preference will “stick” and follow you, thanks to Rail’s routing.