rails 从机架渲染 html

Sea*_*yar 4 ruby rendering rack ruby-on-rails rackattack

我正在使用机架攻击。如果有人超过限制,我将使用以下代码:

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

当 sby 超过原始响应的 POST 请求的限制时respond_to :html,渲染429.html工作正常。当响应的 POST 请求超过限制respond_to :js时,屏幕上没有任何反应,但如果我查看日志,一切似乎都很好:

Rendered public/429.html (1.4ms)
Run Code Online (Sandbox Code Playgroud)

429.html在 的情况下如何显示js response?是否有可能以error messages某种方式从这个机架代码传递到 rails 应用程序?如果不是那么复杂,我可能会更改为error messagesfrom rendering

小智 6

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

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

  • 对于 Rails 6,您需要使用 `ActionView::Base.empty` 而不是 `ActionView::Base.new` (2认同)