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

后端 未结 15 1297
余生分开走
余生分开走 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:23

    I solved this problem by using this before_filter in my ApplicationController:

    def set_mobile_format
      request.formats.unshift(Mime::MOBILE) if mobile_client?
    end
    

    This puts the mobile format to the front of the list of acceptable formats. So, the Resolver prefers .mobile.erb templates, but will fall back to .html.erb if no mobile version is found.

    Of course, for this to work you need to implement some kind of #mobile_client? function and put Mime::Type.register_alias "text/html", :mobile into your config/initializers/mime_types.rb

提交回复
热议问题