Rails 从 Rack 渲染 HTML

4

我正在使用Rack Attack。如果有人超过了限制,我将使用以下代码:

Rack::Attack.throttled_response = lambda do |env|
  [429, {}, [ActionView::Base.new.render(file: 'public/429.html')]]
end

当某人超过POST请求的限制时,如果原始响应为respond_to:html,那么429.html的渲染工作正常。但是,如果POST请求响应respond_to:js超过了限制,则屏幕上什么都不会发生,但如果我查看日志,一切似乎都很正常:

Rendered public/429.html (1.4ms)

如果出现js响应,我该如何显示429.html?有没有可能从这个rack代码中传递错误信息到rails app中?如果不是太复杂的话,我可以改用呈现错误信息

1个回答

7
Rack::Attack.throttled_response = lambda do |env|
  html = ActionView::Base.new.render(file: 'public/429.html')
  [503, {'Content-Type' => 'text/html'}, [html]]
end

您可以在第二个参数中设置任何响应内容类型。


2
对于Rails 6,您应该使用ActionView::Base.empty而不是ActionView::Base.new - venables

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接