instance method formats=

Ruby on Rails 6.0.6

Since v4.0.13

Available in: v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

formats=(extensions)

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 fallback 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

Parameters

extensions req
Source
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 142
      def formats=(extensions)
        parameters[:format] = extensions.first.to_s
        set_header "action_dispatch.request.formats", extensions.collect { |extension|
          Mime::Type.lookup_by_extension(extension)
        }
      end

Defined in actionpack/lib/action_dispatch/http/mime_negotiation.rb line 142 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Http::MimeNegotiation

Type at least 2 characters to search.

↑↓ navigate · open · esc close