方法
- H
实例公共方法
head(status, options = nil) 链接
返回一个没有内容(只有头部)的响应。options 参数被解释为头部名称和值的哈希。这使您可以轻松地返回仅包含重要头部的响应。
head :created, location: person_path(@person) head :created, location: @person
它也可以用来返回异常情况。
return head(:method_not_allowed) unless request.post? return head(:bad_request) unless valid_request? render
有关有效的 status 符号的完整列表,请参阅 Rack::Utils::SYMBOL_TO_STATUS_CODE。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal/head.rb, line 23 def head(status, options = nil) if status.is_a?(Hash) raise ArgumentError, "#{status.inspect} is not a valid value for `status`." end raise ::AbstractController::DoubleRenderError if response_body status ||= :ok if options location = options.delete(:location) content_type = options.delete(:content_type) options.each do |key, value| headers[key.to_s.split(/[-_]/).each { |v| v[0] = v[0].upcase }.join("-")] = value.to_s end end self.status = status self.location = url_for(location) if location if include_content?(response_code) unless self.media_type self.content_type = content_type || ((f = formats) && Mime[f.first]) || :html end response.charset = false end self.response_body = "" true end