# Instance var getting lost during template render

**URL:** <https://discuss.rubyonrails.org/t/instance-var-getting-lost-during-template-render/47095>\
**Category:** rubyonrails-talk\
**Created:** [October 26, 2009, 11:35am UTC](https://discuss.rubyonrails.org/t/instance-var-getting-lost-during-template-render/47095 "2009-10-26T11:35:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 26, 2009, 11:35am UTC](https://discuss.rubyonrails.org/t/instance-var-getting-lost-during-template-render/47095/1 "2009-10-26T11: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:

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

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

&nbsp;&nbsp;def bad\_url(exception) &nbsp;&nbsp;&nbsp;&nbsp;@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." &nbsp;&nbsp;&nbsp;&nbsp;render :template =\> "errors/index", :layout =\> "v4-2col-paranoid" and return &nbsp;&nbsp;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:** ![Leonardo\_Mateo](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Leonardo\_Mateo](https://discuss.rubyonrails.org/u/Leonardo_Mateo)\
**Post date:** [October 26, 2009, 1:00pm UTC](https://discuss.rubyonrails.org/t/instance-var-getting-lost-during-template-render/47095/2 "2009-10-26T13:00:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 26, 2009, 1:04pm UTC](https://discuss.rubyonrails.org/t/instance-var-getting-lost-during-template-render/47095/3 "2009-10-26T13:04:50Z")

</div>

Leonardo Mateo wrote:
