Redirecting to a 500-page page when an AJAX call fails in Ruby on Rails

I am working with an application built into Ruby on Rails with very poor error handling right now. If the controller method is executed via ajax, and this method results in 500 (or 404 or any other answer), then the page 500.html is displayed and returned as the result in the AJAX request. Obviously, javascript does not know what to do with this HTML, and the web page looks like it is just waiting for an answer.

Is there an easy way in rails to render the error.rjs template anytime an error occurs during an AJAX call?

+6
ruby ajax ruby-on-rails rjs
source share
4 answers

You can use response_to inside the rescue_action or rescue_action_in_public controller. Consider the following controller:

class DefaultController < ApplicationController def index raise "FAIL" end def rescue_action(exception) respond_to do |format| format.html { render :text => "Rescued HTML" } format.js { render :action => "errors" } end end end 
+3
source share

I solved a similar problem with authorization. I created a simple authorization controller with this action:

  def unauthorizedxhr render :update do |page| page.replace_html("notice", :partial=>"unauthorizedxhr") page.show("notice") end end 

Here's the pattern:

 <% if flash[:notice] -%> <div id="noticexhr"><%= flash[:notice] %></div> <% end -%> 

When authorization failed with an error in the controller, I redirected to: controller => "authorization" ,: action => "unauthorizedxhr" after setting the flash [: notice] value. This allowed me to customize the message sent to the user and handled the display of the message using the rendering code: above.

You can adapt this to your problem by creating an error controller, catching any errors raised in your other controllers, and then simply redirecting: controller => "errors" ,: action => "displayxhr" when the call fails. In this way, you standardized your error reporting mechanism, but you would allow yourself to customize error messages with every action.

You can still use the cpm idea above, but the error display will be handled by separate and separate controller logic. which should make it a little easier to maintain.

Hope this helps. -Chris

+3
source share

This was my final decision:

 def rescue_action_in_public(exception) response_code = response_code_for_rescue(exception) status = interpret_status(response_code) respond_to do |format| format.html { render_optional_error_file response_code} format.js { render :update, :status => status do |page| page.redirect_to(:url => error_page_url(status)) end} end end 

Basically this is sent to the correct static HTML page, regardless of whether the request was executed via AJAX or regular GET / POST.

This is NOT used for normal error handling, such as checking, etc. It is used only when something really bad happens - like an unhandled exception.

+1
source share

You can do as below:

in allpication.js

 $(document).ready(function(){ $(document).ajaxError( function(e, xhr, options){ if("500" == xhr.status) { $(location).attr('href','/users/sign_in'); } }); }) 

His work is for me .....

0
source share

All Articles