method

formats=

Importance_2
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Class: ActionDispatch::Http::MimeNegotiation
formats=(extensions) public

Sets the formats by string extensions. This differs from #format= by allowing you to set multiple, ordered formats, which is useful when you want to have a fallback.

In this example, the :iphone format will be used if it’s available, otherwise it’ll fall back to the :html format.

class ApplicationController < ActionController::Base
  before_action :adjust_format_for_iphone_with_html_fallback

  private
    def adjust_format_for_iphone_with_html_fallback
      request.formats = [ :iphone, :html ] if request.env["HTTP_USER_AGENT"][/iPhone/]
    end
end
Show source
Register or log in to add new notes.