Sean Magyar
Sean Magyar

Reputation: 2380

rails rendering html from rack

I'm using rack attack. If somebody exceeds the limit I'm using the following code:

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

When sby exceeds the limit on a POST request where the original response would be respond_to :html then the rendering of the 429.html works fine. When the limit is exceeded by a POST request that responds to respond_to :js then nothing happens on the screen, but if I check out the logs everything seems to be fine:

Rendered public/429.html (1.4ms)

How can I display the 429.html in case of js response? Is it possible to pass down error messages from this rack code to the rails app somehow? I may change to error messages from rendering if it's not that complex.

Upvotes: 4

Views: 1989

Answers (1)

Yang
Yang

Reputation: 389

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

You can set any response content type in the second params.

Upvotes: 7

Related Questions