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
Christopher Hazlett
source share