Rails 3.0 Mobile Website

I was wondering how I'm going to create a mobile version of the Rails 3.0 application.

I saw this post: Mobile views for Ruby on Rails

But I got confused in the response_to method. How does the method know which format to display?

Is it possible to create a method in the application controller to display a mobile layout, and then use the response_to method for each view?

Thanks,

Brian

+4
source share
3 answers
+5
source

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

This works out of the box for common mime types, but you will need to tell your application about your custom ones. In your application controller, you want to define a method that will adjust the format of the internal Rails request review. Then call this method as a before filter. Here is 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 define this new type in config / initializers / mime_types.rb:

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

Then in your controllers you can 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 

It certainly looks elegant, but in practice, I find that I rarely use it for mobile sites. The mobile sites I worked on are generally very different from the "full" sites. In these cases, it makes sense to simply define another group of controllers under the "mobile" namespace.

+2
source

Check out Rails Mobile

I developed this plugin a while ago. The idea of โ€‹โ€‹this plugin is that you can redirect to different controllers or views based on the capabilities of your mobile device through the configuration file of the router.

At the end of routing.rb, add the following lines:

 MobileDispatch::Categories.add do def mobile_classifier(device) "_mobile" end end 

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

This way you can play with routing rules. For example, this line in routing.rb:

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

for a regular user will be interpreted as:

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

and for the mobile user:

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

The strength here is in the mobile_classifier (device) method, where you can return a different classification based on the device object.

so let's say we modify the method to return "_iphone" for all iphone devices and "_android" for all Android mobile phones, then the specified routing line will 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 $ to the end of the overview section of each route (similar to what we did here), you will get different methods in your controller for each device category and different name names for each method (index_iphone.htm. Erb and index_android.ht.erb ) Thus, you have separate views / levels for each category of devices that you defined in your mobile_classifier method.

+1
source

All Articles