名稱空間
方法
- A
- C
- F
- N
- S
- V
常數
BROWSER_LIKE_ACCEPTS | = | /,\s*\*\/\*|\*\/\*\s*,/ |
除非您於清單中加入 /,表示您為瀏覽器並傳送 HTML,否則我們使用正常的內容協商。 |
||
RESCUABLE_MIME_FORMAT_ERRORS | = | [ ActionController::BadRequest, ActionDispatch::Http::Parameters::ParseError, ] |
實例公開方法
accepts() 連結
傳回請求的接受 MIME 型別。
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 42 def accepts fetch_header("action_dispatch.request.accepts") do |k| header = get_header("HTTP_ACCEPT").to_s.strip v = if header.empty? [content_mime_type] else Mime::Type.parse(header) end set_header k, v rescue ::Mime::Type::InvalidMimeType => e raise InvalidType, e.message end end
content_mime_type() 連結
HTTP 請求的 MIME 型別,例如 Mime。
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 24 def content_mime_type fetch_header("action_dispatch.request.content_type") do |k| v = if get_header("CONTENT_TYPE") =~ /^([^,;]*)/ Mime::Type.lookup($1.strip.downcase) else nil end set_header k, v rescue ::Mime::Type::InvalidMimeType => e raise InvalidType, e.message end end
format(_view_path = nil) 連結
傳回請求中使用的格式的 MIME 型別。
GET /posts/5.xml | request.format => Mime[:xml]
GET /posts/5.xhtml | request.format => Mime[:html]
GET /posts/5 | request.format => Mime[:html] or Mime[:js], or request.accepts.first
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 63 def format(_view_path = nil) formats.first || Mime::NullType.instance end
format=(extension) 連結
依據字串副檔名設定格式,可用於強制套用自訂格式,不受副檔名控制。
class ApplicationController < ActionController::Base
before_action :adjust_format_for_iphone
private
def adjust_format_for_iphone
request.format = :iphone if request.env["HTTP_USER_AGENT"][/iPhone/]
end
end
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 115 def format=(extension) parameters[:format] = extension.to_s set_header "action_dispatch.request.formats", [Mime::Type.lookup_by_extension(parameters[:format])] end
formats() 連結
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 67 def formats fetch_header("action_dispatch.request.formats") do |k| v = if params_readable? Array(Mime[parameters[:format]]) elsif use_accept_header && valid_accept_header accepts.dup elsif extension_format = format_from_path_extension [extension_format] elsif xhr? [Mime[:js]] else [Mime[:html]] end v.select! do |format| format.symbol || format.ref == "*/*" end set_header k, v end end
formats=(extensions) 連結
依據字串副檔名設定格式。此點與 format=
的不同處為,可讓您設定多種順序格式,於需要備用時非常有用。
於此範例中,如果 :iphone
格式可用,即使用該格式,否則備用為 :html
格式。
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