views:

529

answers:

2

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.

+5  A: 

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
alas -- see update
James A. Rosen
oh, wait! yes! without the @!
James A. Rosen
+1  A: 

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

render :template => '/errors/not_found', :status => 404, :locals => {:exception => exception}
spilth