Can a mobile mime type fall back to “html” in Rails?

后端 未结 15 1299
余生分开走
余生分开走 2020-12-23 22:17

I\'m using this code (taken from here) in ApplicationController to detect iPhone, iPod Touch and iPad requests:

before_filter :detect_mobile_request, :detec         


        
15条回答
  •  粉色の甜心
    2020-12-23 22:21

    Rails 4.1 includes Variants, this is a great feature that allow you to set different views for the same mime. You can now simply add a before_action and let the variant to do the magic:

    before_action :detect_device_variant
    
    def detect_device_variant
      case request.user_agent
      when /iPad/i
        request.variant = :tablet
      when /iPhone/i
        request.variant = :phone
      end
    end
    

    Then, in your action:

    respond_to do |format|
      format.json
      format.html               # /app/views/the_controller/the_action.html.erb
      format.html.phone         # /app/views/the_controller/the_action.html+phone.erb
      format.html.tablet do
        @some_tablet_specific_variable = "foo"
      end
    end
    

    More info here.

提交回复
热议问题