class

Mime::Type

rails latest stable - Superclass: Object

Encapsulates the notion of a MIME type. Can be used at render time, for example, with:

class PostsController < ActionController::Base
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html
      format.ics { render body: @post.to_ics, mime_type: Mime::Type.lookup("text/calendar")  }
      format.xml { render xml: @post }
    end
  end
end

Constants

ACCEPT_HEADER_REGEXP = /[^,\\s"](?:[^,"]|"[^"]*")*/

MIME_NAME = "[a-zA-Z0-9][a-zA-Z0-9#{Regexp.escape('!#$&-^_.+')}]{0,126}"

MIME_PARAMETER = "\\s*;\\s*#{MIME_NAME}(?:=#{MIME_PARAMETER_VALUE})?"

MIME_PARAMETER_VALUE = "(?:#{MIME_NAME}|\\"[^\\"\\r\\\\\\\\]*\\")"

MIME_REGEXP = /\\A(?:\\*\\/\\*|#{MIME_NAME}\\/(?:\\*|#{MIME_NAME})(?>#{MIME_PARAMETER})*\\s*)\\z/

PARAMETER_SEPARATOR_REGEXP = /;\\s*q="?/

TRAILING_STAR_REGEXP = /^(text|application)\\/\\*/

Attributes

[R]hash
[R]string
[R]symbol
[R]synonyms

Files

  • actionpack/lib/action_dispatch/http/mime_type.rb

Nested classes and modules

2Notes

Can also be used to conditionally apply filters

rob-twf · Mar 11, 20092 thanks

For example:

# Skip login filter if the request is for CSS
before_filter :require_login, :unless => lambda { |controller| controller.request.format.css? }

Calling request.format on the controller returns a Mime::Type object, which can then be queried for mime types, other examples:

controller.request.format.html?
controller.request.format.json?

Setting a custom Content type

schmidt · Nov 3, 2009

The given example seems to be broken. The +:mime_type+ option as well as the [] access on the Mime::Type class are both not working.

The following code allows the custom setting of content types as intended by the original example:

class  PostsController < ActionController::Base
def show
   @post = Post.find(params[:id])

  respond_to do |format|
    format.html
    format.ics { render :text => post.to_ics, :content_type => Mime::Type.lookup("text/calendar")  }
    format.xml { render :xml => @people.to_xml }
  end
end
end