- A
- C
- F
- N
- S
- V
常量
| BROWSER_LIKE_ACCEPTS | = | /,\s*\*\/\*|\*\/\*\s*,/ |
除非您在列表中包含 /,否则我们使用普通的 MIME 类型协商,在这种情况下,我们假定您是浏览器并发送 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 68 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 174 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 72 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
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 194 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
negotiate_mime(order) 链接
返回与提供的 MIME 类型数组匹配的第一个 MIME 类型。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 202 def negotiate_mime(order) formats.each do |priority| if priority == Mime::ALL return order.first elsif order.include?(priority) return priority end end order.include?(Mime::ALL) ? format : nil end
should_apply_vary_header?() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 214 def should_apply_vary_header? !params_readable? && use_accept_header && valid_accept_header end
variant() 链接
返回响应模板的变体,作为 ActiveSupport::ArrayInquirer 的实例。
request.variant = :phone request.variant.phone? # => true request.variant.tablet? # => false request.variant = [:phone, :tablet] request.variant.phone? # => true request.variant.desktop? # => false request.variant.any?(:phone, :desktop) # => true request.variant.any?(:desktop, :watch) # => false
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 159 def variant @variant ||= ActiveSupport::ArrayInquirer.new end
variant=(variant) 链接
设置响应模板的变体。
在确定要渲染哪个模板时,Action View 将合并请求中的所有变体。例如,如果 ArticlesController#index 操作需要响应 request.variant = [:ios, :turbo_native],它将渲染在以下列表中找到的第一个模板文件:
-
app/views/articles/index.html+ios.erb -
app/views/articles/index.html+turbo_native.erb -
app/views/articles/index.html.erb
变体为视图渲染提供上下文。变体名称是任意的,可以传达从请求的平台(:android、:ios、:linux、:macos、:windows)到其浏览器(:chrome、:edge、:firefox、:safari),再到用户类型(:admin、:guest、:user)。
注意:添加许多与现有模板文件相似的新变体模板会使维护视图代码更加困难。
Parameters¶ ↑
-
variant- 用于渲染响应模板的变体的符号名称或符号名称数组
示例¶ ↑
class ApplicationController < ActionController::Base before_action :determine_variants private def determine_variants variants = [] # some code to determine the variant(s) to use variants << :ios if request.user_agent.include?("iOS") variants << :turbo_native if request.user_agent.include?("Turbo Native") request.variant = variants end end
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/mime_negotiation.rb, line 137 def variant=(variant) variant = Array(variant) if variant.all?(Symbol) @variant = ActiveSupport::ArrayInquirer.new(variant) else raise ArgumentError, "request.variant must be set to a Symbol or an Array of Symbols." end end