4

I was wondering how I go about creating a mobile version of a Rails 3.0 application.

I saw this post: Mobile version of views for Ruby on Rails

But I am confused on the respond_to method. How does the method know which format to render?

Would I create a method in my application controller to render a mobile layout and then for each view use the respond_to method?

Thank you,

Brian

1

3 Answers 3

5

Ryan Bates has done a great tutorial

http://railscasts.com/episodes/199-mobile-devices

1
  • Thanks! That tutorial helped a lot.
    – Brian
    Oct 19, 2011 at 15:06
2

The respond_to method will choose according to the current request's mime type.

This works out of the box for common mime types, but you'll need to tell your application about your custom ones. In your application controller, you'll want to define a method that will adjust the format of Rails' internal reprensentation of the request. Then, call that method as a before filter. Here's an example:

class ApplicationController < ActionController::Base
  before_filter :adjust_for_mobile

  def adjust_for_mobile
    request.format = :mobile if mobile_request
  end

  # You'll also need to define the mobile_request method
  # using whatever strategy you want to tell if a request
  # is from a mobile client or not
  def mobile_request
    true
  end
end

Make sure you've defined this new type in config/initializers/mime_types.rb:

Mime::Type.register "text/html", :mobile

Then, in your controllers, you'll be able to use the 'mobile' format:

class FoosController < ApplicationController
  def index
    @foos = Foo.all

    respond_to do |format|
      format.html   # index.html.erb
      format.mobile # index.mobile.erb
    end
  end
end

This sure looks elegant and all but in practice, I find that I rarely use it for mobile sites. The mobile sites I've been working on are generally quite different from the 'complete' sites. In those cases it makes sense to just define another bunch of controllers under a 'mobile' namespace.

1

Have a look at Rails Mobile

I have developed that plugin a while back. The idea behind that plugin is you can redirect to different controllers or views based on your mobile device capabilities through your router config file.

At the end of the routing.rb add these lines:

MobileDispatch::Categories.add do

def mobile_classifier(device)
  "_mobile"
end
end

These lines define a new substring for all mobile devices which will be stored in $ variable for each request in the rouging.rb file.

That way you can play with your routing rules. For instance this line in routing.rb:

match '/photo/:id', :to => "photo#index$", :classifier => :mobile_classifier

for a normal user would be interpreted as:

match '/photo/:id', :to => "photo#index", :classifier => :mobile_classifier

while for a mobile user as:

match '/photo/:id', :to => "photo#index_mobile", :classifier => :mobile_classifier

The power here is in mobile_classifier(device) method where you can return different classification based on device object.

so let say we modify the method to return "_iphone" for all iphone devices and "_android" for all android mobiles, then the above routing line would be interpreted as:

match '/photo/:id', :to => "photo#index_iphone", :classifier => :mobile_classifier

match '/photo/:id', :to => "photo#index_android", :classifier => :mobile_classifier

If you add the $ to the end of view part of each route (similar to what we did here) you will get different methods in your controller for each category of devices and different view names for each method (index_iphone.htm.erb and index_android.ht.erb) This way you have seperate views/layers for each device category that you defined in your mobile_classifier method.

3
  • So using the plugin the app still calls the method, but if the request is from a mobile device it renders a mobile view? Does it automatically detect a mobile device? What mobile devices does this plugin support?
    – Brian
    Oct 7, 2011 at 0:51
  • Sorry, also what about mobile layouts? Is there a way to render a mobile layout using this plugin or would I just add a method to my application controller?
    – Brian
    Oct 7, 2011 at 0:53
  • Sorry I didn't see your second question. please check my updated answer
    – Reza
    Oct 12, 2011 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.