在我的 rails 4 应用程序中,我想同时回复html
forhtml
和js
request。在请求html
输入的那一刻,渲染工作正常,但是当请求时js
,html文件不会在屏幕上渲染(尽管在命令行中它说它已经渲染)。
限制请求有不同的场景,因此节流代码也可以由html POST
请求触发js POST
。
Rack::Attack.throttle(key, limit: from_config(key, :limit), period: from_config(key, :period)) do |req|
if req.path.ends_with?(from_config(key, :path).to_s) && from_config(key, :method) == req.env['REQUEST_METHOD']
### This is the snippet I try to change the req type with but not working
if req.media_type == 'application/javascript'
req.media_type = 'text/html'
end
##### till here
req.ip
end
end
这是我要渲染的内容。如您所见,这是html
响应。
Rack::Attack.throttled_response = lambda do |env|
[429, {}, [ActionView::Base.new.render(file: 'public/429.html', content_type: 'text/html')]]
end
我应该怎么办?
更新
这是我的最新版本,但不知道如何检查请求内容类型:
Rack::Attack.throttled_response = lambda do |env|
retry_after = (env['rack.attack.match_data'] || {})[10]
if env['rack.attack.content_type'] == 'text/html'
[429, {'Retry-After' => retry_after.to_s}, [ActionView::Base.new.render(file: 'public/429.html', content_type: 'text/html')]]
elsif env['rack.attack.content_type'] == 'application/javascript'
[429, {'Retry-After' => retry_after.to_s}, window.location.href = '/429.html']
end
end