4

I have an error handling method in my ApplicationController:

rescue_from ActiveRecord::RecordNotFound, :with => :not_found

def not_found(exception)
  @exception = exception
  render :template => '/errors/not_found', :status => 404
end

In RAILS_ROOT/app/views/errors/not_found.html.erb, I have this:

<h1>Error 404: Not Found</h1>
<%= debug @exception %>

But @exception is always nil there. I've tried debug assigns, but that's always {}. Do assigns not get copied when calling render :template? If so, how can I get them?

I'm on edge Rails.

James A. Rosen
  • 64,193
  • 61
  • 179
  • 261

2 Answers2

5

That's odd, and I don't know why. As an alternative, have you tried passing the exception as an explicit local?

def not_found(exception)
  render :template => '/errors/not_found', 
         :status   => 404, 
         :locals   => {:exception => exception}
end

and the view:

<h1>Error 404: Not Found</h1>
<%= debug exception %> <!-- Note no '@' -->
Avdi
  • 18,340
  • 6
  • 53
  • 62
1

From the API documentation for ActionController::Base it looks like you should try:

render :template => '/errors/not_found', :status => 404, :locals => {:exception => exception}
Brian Kelly
  • 5,564
  • 4
  • 27
  • 31