Instance var getting lost during template render

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

On Mon, Oct 26, 2009 at 12:35 PM, Max W.
[email protected] 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 ~

Leonardo M. wrote:

On Mon, Oct 26, 2009 at 12:35 PM, Max W.
[email protected] 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?