Skip to content

How To: Make Devise work with other formats like mobile, iPhone and iPad (Rails specific)

Garrett George edited this page Mar 16, 2021 · 3 revisions
  1. Make your app render Apple iOS templates for iOS devices like iPhone, iPad, iPod Touch, etc.

    Add the following to app/controllers/application_controller.rb:

    # app/controllers/application_controller.rb
    
    class ApplicationController < ActionController::Base
      before_filter :adjust_format_for_iphone
    
      private
    
      def adjust_format_for_iphone    
        request.format = :ios if request.env["HTTP_USER_AGENT"] =~ %r{Mobile/.+Safari}
      end
    end
  2. Set Devise to display views for iOS.

    Add every desired format to config.navigational_formats in config/initializers/devise.rb.

    # config/initializers/devise.rb
    
    ...
    config.navigational_formats = [:"*/*", "*/*", :html, :ios]
    ...
  3. Create each view you need for Devise (login form, signup form, etc.).

    Name your view something like new.**ios**.erb, e.g. app/views/devise/sessions/new.ios.erb for login form.

  4. Create an initializer in config/initializers (e.g. config/initializers/mobile_formats.rb) with the following:

    # config/initializers/mobile_formats.rb
    
    ActionController::Responder.class_eval do
      alias :to_ios :to_html
    end
  5. Add your format to config/initializers/mime_types.rb

    # config/initializers/mime_types.rb
    
    Mime::Type.register_alias "text/html", :ios
  6. Note that if you use OAUTH for Facebook you will need to configure it to use facebook's mobile authentication path. This post explains how.

Clone this wiki locally