# Instance var getting lost during template render

**URL:** <https://www.ruby-forum.com/t/instance-var-getting-lost-during-template-render/177893>\
**Category:** Rails\
**Created:** [October 26, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/instance-var-getting-lost-during-template-render/177893 "2009-10-26T12:35:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 26, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/instance-var-getting-lost-during-template-render/177893/1 "2009-10-26T12:35:37Z")

</div>

hey all

I’m using rescue\_from to catch exceptions and render a ‘whoops - we had  
a problem’ kind of page. Before i render the page, i set an instance  
variable @message which i want to use on the page. But, it’s not being  
carried through for some reason - can anyone see why? Here’s the setup  
i have:

rescue\_from Exception, :with =\> :any\_exception  
rescue\_from ActionController::RoutingError, :with =\> :bad\_url  
rescue\_from ActionController::MethodNotAllowed, :with =\> :bad\_url  
rescue\_from ActionController::UnknownAction, :with =\> :bad\_url

…  
private  
def any\_exception(exception)  
@message = “We’re sorry, but something went wrong.”  
render :template =\> “errors/index”, :layout =\> “v4-2col-paranoid”  
and return  
end

def bad\_url(exception)  
@message = “I’m sorry, but this url is invalid. If you clicked on  
the url in an email, or copied it from somewhere, then please check that  
it was complete. If you got here by clicking on a link inside the site,  
then we apologise.”  
render :template =\> “errors/index”, :layout =\> “v4-2col-paranoid”  
and return  
end

So, i’m triggering the ‘any\_exception’ method by putting \<% raise “BOOM”  
%\> in one of my views. I can see with logging that the any\_exception  
method is called, and it does render the errors/index page. However, on  
the page, @message is nil - it’s like it’s got lost somewhere between  
being set and being used. Can anyone see what i’m doing wrong?

thanks, max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 26, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/instance-var-getting-lost-during-template-render/177893/2 "2009-10-26T14:01:56Z")

</div>

On Mon, Oct 26, 2009 at 12:35 PM, Max W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rescue\_from ActionController::RoutingError, :with =\> :bad\_url
> 
> %\> in one of my views. I can see with logging that the any\_exception  
> method is called, and it does render the errors/index page. However, on  
> the page, @message is nil - it’s like it’s got lost somewhere between  
> being set and being used. Can anyone see what i’m doing wrong?

You should pass the variable with the locals parameter.  
render :template =\> “errors/index”, :layout =\> “v4-2col-paranoid”,  
:locals =\> {:message =\> @message}

And in the view, refer to it as message.

## Hope it helps.

Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 26, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/instance-var-getting-lost-during-template-render/177893/3 "2009-10-26T14:04:50Z")

</div>

Leonardo M. wrote:

> On Mon, Oct 26, 2009 at 12:35 PM, Max W.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½rescue\_from ActionController::RoutingError, :with =\> :bad\_url
> > 
> > %\> in one of my views. ï¿½I can see with logging that the any\_exception  
> > method is called, and it does render the errors/index page. ï¿½However, on  
> > the page, @message is nil - it’s like it’s got lost somewhere between  
> > being set and being used. ï¿½Can anyone see what i’m doing wrong?
> 
> You should pass the variable with the locals parameter.  
> render :template =\> “errors/index”, :layout =\> “v4-2col-paranoid”,  
> :locals =\> {:message =\> @message}
> 
> And in the view, refer to it as message.
> 
> ## Hope it helps.
> 
> Leonardo M…  
> There’s no place like ~

Ah… I thought that was just for partials. If i set an instance var  
and then call render :template in a regular controller action, then it  
works fine. Is the behaviour different when you call render from a  
private controller method than when you call it from a regular action?
