Ruby on rails 使用send_数据时如何设置Expires:header

Ruby on rails 使用send_数据时如何设置Expires:header,ruby-on-rails,http-caching,Ruby On Rails,Http Caching,我的控制器中有一种方法,它使用如下所示的send_数据: def show expires_in 10.hours, :public => true send_data my_image_generator, :filename => "image.gif", :type => "image/gif" end HTTP/1.1 200 OK Connection: close Date: Fri, 25 Jun 2010 10:41:22 GMT ETag: "885

我的控制器中有一种方法,它使用如下所示的send_数据:

def show
  expires_in 10.hours, :public => true
  send_data my_image_generator, :filename => "image.gif", :type => "image/gif"
end
HTTP/1.1 200 OK
Connection: close
Date: Fri, 25 Jun 2010 10:41:22 GMT
ETag: "885d75258e9306c46a5dbfe3de44e581"
Content-Transfer-Encoding: binary
X-Runtime: 143
Content-Type: image/gif
Content-Disposition: inline; filename="image.gif"
Content-Length: 1277
Cache-Control: max-age=36000, public
在中使用expires\u会导致发送如下标题:

def show
  expires_in 10.hours, :public => true
  send_data my_image_generator, :filename => "image.gif", :type => "image/gif"
end
HTTP/1.1 200 OK
Connection: close
Date: Fri, 25 Jun 2010 10:41:22 GMT
ETag: "885d75258e9306c46a5dbfe3de44e581"
Content-Transfer-Encoding: binary
X-Runtime: 143
Content-Type: image/gif
Content-Disposition: inline; filename="image.gif"
Content-Length: 1277
Cache-Control: max-age=36000, public
我想做的是添加一个标题,如
Expires:(某个确切日期)
,以防止用户代理重新验证。但我不知道如何将send_数据集设置为该标题


我想我可以在
response.headers
散列中明确地设置它,但肯定有一个包装器(或什么的)?

显然,似乎没有办法通过过期来发送数据-相反,你必须自己在
response.headers
中设置它,并注意适当地格式化日期:

response.headers["Expires"] = CGI.rfc1123_date(Time.now + period)

请注意,
Cache Control
标题中的
max age
指令将覆盖
Expires
标题(如果两者都存在)。有关更多详细信息,请参阅。

我遇到了这种语法,我喜欢它:-)


您的问题中的代码实际上应该适用于最新的Rails:

`expires_in 10.hours, :public => true`

这不正是asker在其原始代码中所做的吗?事实上,我发现这在Rails 4.2上是可行的,也许Rails修复了一个bug?只需确保避免使用相同的方法设置任何手动标题。它还有一个额外的好处,就是允许您将其声明为公共。您是将其放在发送数据之前还是之后?在
发送数据之前添加
;一旦调用了
send\u data
,设置标题就没有意义了。你需要
要求“时间”
才能使用它。@NicolasGoy你确定rails就是这样吗?(我刚刚在Rails 4.2.0.beta4上尝试了这个功能,它不需要
时间
)这是因为Rails需要它。但是试一下irb。