Rails and Mobile Content

Couple of quick tips for creating content meant for mobile devices using Ruby on Rails:

  • You’ll probably want to setup a new default layout for all templates. You can do that by creating app/views/layouts/application.rhtml and populating it with whatever you want your default layout to be. For instance here’s an unstyled version of the mostest simplest template that Luca posted with GAP as a default rhtml layout:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC
    "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
    "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
    <html
    xmlns="http://www.w3.org/1999/xhtml"
    xml:lang="en">
      <head>
        <title>Valentine</title>
      </head>
      <body>
        <%= @content_for_layout %>
      </body>
    </html>
    
  • Adaptation of the content type based on the mime types the device claims to accept is also pretty much a necessity. I’m going off the MIME types post pointed to from GAP for this also in creating an ApplicationController that sets content type based on the accept header sent by the browser:
    class ApplicationController < ActionController::Base
      before_filter :set_content_type
      def set_content_type
        if @request.accepts.include?( Mime::Type.parse( 'application/vnd.wap.xhtml+xml' ) )
          type = 'application/vnd.wap.xhtml+xml'
        elsif @request.accepts.include?( Mime::Type.parse( 'application/xhtml+xml' ) )
          type = 'application/xhtml+xml'
        else
          type = 'text/html'
        end
        @headers["Content-Type"] = type + "; charset=utf-8"
      end
    end
    

    The order for the checks I’m sure is something that just about everyone will have their own opinion about, so I stuck with what that post had.

This entry was posted in ThisIsMobility. Bookmark the permalink.

3 Responses to Rails and Mobile Content

  1. Pingback: Web es móvil

  2. Trilok Agarwal says:

    used it render the same pages on mobile and stand alone web works just fine
    just that @request and @headers have been deprecated in recent rails. So use request and headers method instead.

  3. One thing I found out while working on a mobile rails site was that some lower end phones do not like using the redirect_to method with the :controller and :action arguments, but allow hard coding the redirect_to a specific address like eg:

    redirect_to “http://m.bublit.com/”

    Not the best way of doing things, I know, but it seems to work. I guess you cannot compete with real phone testing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">