Yur*_*dow 8 ruby-on-rails http-headers
在早期版本中,这可以工作:
ActionController::Renderers.add(:foo) do | data, options |
self.content_type = 'application/foo; bar=1'
end
Run Code Online (Sandbox Code Playgroud)
在4.2.4中,这会导致Content-Type标头为空.但是,以下工作,即将Content-Type标头设置为分配给content_type的字符串:
ActionController::Renderers.add(:foo) do | data, options |
self.content_type = 'application/foo'
end
Run Code Online (Sandbox Code Playgroud)
我知道的另一种方法是,在渲染上设置content_type,似乎没有结果,也就是说,render('foo', content_type: 'application/foo')没有设置标题(不要介意尝试application/foo; bar = 1.)
首先看一下文档(第2.2.13.1节):
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
他们给出的示例使用您的替代方法,content_type在使用时进行设置render:
render file: filename, content_type: "application/rss"
Run Code Online (Sandbox Code Playgroud)
我在vanilla Rails 4.2.4应用程序中测试了这个策略.这是我定义控制器的方式:
class WelcomeController < ApplicationController
def index
render inline: 'Hello World', content_type: 'application/foo; bar=1'
end
end
Run Code Online (Sandbox Code Playgroud)
这是我在Chrome的网络检查器中看到的,当我点击该操作时,请注意Content-Type响应标题:
一般
Remote Address:[::1]:3000
Request URL:http://localhost:3000/
Request Method:GET
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)
响应标题
Cache-Control:max-age=0, private, must-revalidate
Connection:Keep-Alive
Content-Length:11
Content-Type:application/foo; bar=1; charset=utf-8
Date:Tue, 29 Sep 2015 02:53:39 GMT
Etag:W/"b10a8db164e0754105b7a99be72e3fe5"
Server:WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13)
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Request-Id:3825d446-44dc-46fa-8aed-630dc7f001ae
X-Runtime:0.022774
X-Xss-Protection:1; mode=block
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12730 次 |
| 最近记录: |