【问题标题】:rails rendering html from rackrails 从机架渲染 html
【发布时间】:2016-03-25 17:40:19
【问题描述】:

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

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

当 sby 超过原始响应为 respond_to :html 的 POST 请求的限制时,429.html 的呈现工作正常。当响应 respond_to :js 的 POST 请求超出限制时,屏幕上什么也没有发生,但如果我查看日志,一切似乎都很好:

Rendered public/429.html (1.4ms)

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

【问题讨论】:

    标签: ruby-on-rails ruby rendering rack rackattack


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

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

    【讨论】:

    • 对于 Rails 6,您需要使用 ActionView::Base.empty 而不是 ActionView::Base.new
    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多