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 -- Posted via http://www.ruby-forum.com/.
On Mon, Oct 26, 2009 at 12:35 PM, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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? >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 Mateo. There''s no place like ~
Leonardo Mateo wrote:> On Mon, Oct 26, 2009 at 12:35 PM, Max Williams > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Mateo. > 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? -- Posted via http://www.ruby-forum.com/.