2

I am currently working on a Rails 4 app and have come to the point where I want to display a custom error page to the user when any exception occurs. Currently it is using the 500.HTML in the public directory.

How can I get the app to render an erb file instead as I want to have some dynamic stuff on the page.

Thanks for you help, Alex

1
  • 1
    I just found this set of instructions: makandracards.com/makandra/… It probably works with Rails 4 as well Commented Apr 24, 2014 at 6:28

2 Answers 2

6

ApplicationController have method rescue_from rescue exceptions raised in controller actions:

in application_controller.rb:

class ApplicationController < ActionController::Base

  rescue_from YouAwesome::Exception do |exception|
    render_403(exception)
  end

  def render_403(exception)
    logger.warn("Message for log.")
    @error_message = exception.message
    respond_to do |format|
      format.html { render template: 'errors/errors', layout: false, status: 500 }
      format.all { render nothing: true, status: 500 }
    end
  end

end

Now you can create views in errors folder errors.html.erb and render @error_message variable. That it.

Sign up to request clarification or add additional context in comments.

Comments

2

There's an awesome tutorial by Ryan which explains exception handling in rails, have a look at this one http://railscasts.com/episodes/53-handling-exceptions

3 Comments

this tutorial very old, now rails have flexible method for rescue exception.
There's revised version of the tutorial
on your link tutorial date Jul 04, 2007 , put revised version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.