Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 457
Sean Magyar Avatar asked Mar 12 '23 19:03

Sean Magyar


1 Answers

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.

like image 132
Yang Avatar answered Mar 20 '23 08:03

Yang