Hi, Whats the best way to catch an ''Unknown action'' so I can replace the "No action responded to view_foo" response with something nicer? Thanks Nev
On 4/13/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote:> Hi, > > Whats the best way to catch an ''Unknown action'' so I can replace the "No > action responded to view_foo" response with something nicer?When you change to production environment, that message will be replaced with a simple HTTP 404 error message, which you can edit in public/404.html -- Urban Artography http://artography.ath.cx
We''ve also found it handy to catch other exceptions that are effectively 404''s, like invalid IDs being passed through the URL and tripping up WhateverClass.find methods. Easy to do by overriding the rails rescue method in your application controller: def rescue_action_in_public(exception) case exception when ::ActiveRecord::RecordNotFound, ::ActionController::UnknownAction render "404", "404 Not Found" return # ...more special casing here end end Capturing these also allows you to render a railsy 404 page rather than a flat file. Todd On Apr 13, 2005, at 10:06 PM, Rob Park wrote:> On 4/13/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote: >> Hi, >> >> Whats the best way to catch an ''Unknown action'' so I can replace the >> "No >> action responded to view_foo" response with something nicer? > > When you change to production environment, that message will be > replaced with a simple HTTP 404 error message, which you can edit in > public/404.html > > -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 14.4.2005, at 08:06, Rob Park wrote:> On 4/13/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote: >> Hi, >> >> Whats the best way to catch an ''Unknown action'' so I can replace the >> "No >> action responded to view_foo" response with something nicer? > > When you change to production environment, that message will be > replaced with a simple HTTP 404 error message, which you can edit in > public/404.htmlA follow-up: what would be the right/easiest way to catch routing errors and show a 404 instead? No even production system throws a server error to the user. //jarkko> > -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Another followup ... I''ve thought about this now and I''d really like to be able to catch specific exceptions like ''action not found'' or ''route not found'' at development time, just like I can catch ''record not found''. I see ''rescue_action_in_public'' as final rescue point for exceptions that I didn''t code for. Nev -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jarkko Laine Sent: Thursday, 14 April 2005 8:05 PM To: Rob Park; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Catching ''unknown action'' ? On 14.4.2005, at 08:06, Rob Park wrote:> On 4/13/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote: >> Hi, >> >> Whats the best way to catch an ''Unknown action'' so I can replace the >> "No action responded to view_foo" response with something nicer? > > When you change to production environment, that message will be > replaced with a simple HTTP 404 error message, which you can edit in > public/404.htmlA follow-up: what would be the right/easiest way to catch routing errors and show a 404 instead? No even production system throws a server error to the user. //jarkko> > -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi
Ok, I''ve just read through ''rescue.rb'' and it looks like I can simply override rescue_action(exception) with something like this: In application.rb Protected def rescue_action(exception) case exception when RoutingError, UnknownAction then # #handle these my way, ie #redirect to nice oops screen else super end end Nev -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Neville Burnell Sent: Friday, 22 April 2005 2:59 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org; Rob Park Subject: RE: [Rails] Catching ''unknown action'' ? Another followup ... I''ve thought about this now and I''d really like to be able to catch specific exceptions like ''action not found'' or ''route not found'' at development time, just like I can catch ''record not found''. I see ''rescue_action_in_public'' as final rescue point for exceptions that I didn''t code for. Nev -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jarkko Laine Sent: Thursday, 14 April 2005 8:05 PM To: Rob Park; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Catching ''unknown action'' ? On 14.4.2005, at 08:06, Rob Park wrote:> On 4/13/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote: >> Hi, >> >> Whats the best way to catch an ''Unknown action'' so I can replace the >> "No action responded to view_foo" response with something nicer? > > When you change to production environment, that message will be > replaced with a simple HTTP 404 error message, which you can edit in > public/404.htmlA follow-up: what would be the right/easiest way to catch routing errors and show a 404 instead? No even production system throws a server error to the user. //jarkko> > -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Well, this code works just fine for ''UnknownAction'', wont catch a "RoutingError" :( def rescue_action(exception) case exception when ::ActionController::RoutingError, ::ActionController::UnknownAction then render "something_nice" else super end end -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Neville Burnell Sent: Friday, 22 April 2005 3:20 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails] Catching ''unknown action'' ? Ok, I''ve just read through ''rescue.rb'' and it looks like I can simply override rescue_action(exception) with something like this: In application.rb Protected def rescue_action(exception) case exception when RoutingError, UnknownAction then # #handle these my way, ie #redirect to nice oops screen else super end end Nev -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Neville Burnell Sent: Friday, 22 April 2005 2:59 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org; Rob Park Subject: RE: [Rails] Catching ''unknown action'' ? Another followup ... I''ve thought about this now and I''d really like to be able to catch specific exceptions like ''action not found'' or ''route not found'' at development time, just like I can catch ''record not found''. I see ''rescue_action_in_public'' as final rescue point for exceptions that I didn''t code for. Nev -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jarkko Laine Sent: Thursday, 14 April 2005 8:05 PM To: Rob Park; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Catching ''unknown action'' ? On 14.4.2005, at 08:06, Rob Park wrote:> On 4/13/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote: >> Hi, >> >> Whats the best way to catch an ''Unknown action'' so I can replace the >> "No action responded to view_foo" response with something nicer? > > When you change to production environment, that message will be > replaced with a simple HTTP 404 error message, which you can edit in > public/404.htmlA follow-up: what would be the right/easiest way to catch routing errors and show a 404 instead? No even production system throws a server error to the user. //jarkko> > -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote:> Well, this code works just fine for ''UnknownAction'', wont catch a > "RoutingError" :(You could define a catch-all route at the bottom of routes.rb and direct it to a controller/method that outputs whatever you want to output for missing routes, like so: map.connect "*whatever", :controller => "some_controller", :action => "routing_error" -- Regards, Stian Grytøyr