..."? I have an error handling method in my ApplicationController: rescue_from ActiveRec...">

How do I access the "assigns" after "render: template => ..."?

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 always nil . I tried debug assigns , but always {} . Are assignments assigned when calling render :template ? If so, how can I get them?

I am on the verge of Rails.

+4
source share
2 answers

This is strange, and I don’t know why. Alternatively, did you try to pass the exception as explicit local?

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

and view:

 <h1>Error 404: Not Found</h1> <%= debug exception %> <!-- Note no '@' --> 
+5
source

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

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

All Articles