Hello all, I just started learning rails last Friday and I think I''ve made some pretty good advancements in understanding Ruby/Rails. However I''ve run into an issue for which I cannot find the answer. I''ve been searching for a way to catch Routing errors. Here''s what I''m doing (trying to do) in routes.rb (what I added) map.connect '''', :controller => ''site'', :action => ''index'' map.connect '':client_name/'', :controller => ''site'', :action => ''index'' map.connect '':client_name/:controller/:action'' the site controller defines the actions and redirects for the client: def index redirect_to(:client_name => site.subdomain, :controller => ''home'', :action => ''index'') end this ends up with a pretty url of the type: www.mysite.com/client1/home/index everything is looking good, but I want to be able to catch routing errors and redirect them to the home page or show a nice 404 message redirect_to(:client_name => Site.find(1).subdomain, :controller => ''home'', :action = ''index'') But what I get when i type in a bogus url such as www.mysite.com/asdf/fs is the following: Routing Error No route for path: "asdf/fs" Failure reasons: 1. <ActionController::Routing::Route "" when {:controller=>"site", :action=>"index"}> failed because unused components were left: asdf/fs 2. <ActionController::Routing::Route ":client_name" when {:controller=>"site", :action=>"index"}> failed because unused components were left: fs 3. <ActionController::Routing::Route ":client_name/:controller/:action" || {:action=>"index"}> failed because no controller found at subpath fs 4. <ActionController::Routing::Route ":controller/service.wsdl" when {:action=>"wsdl"}> failed because no controller found at subpath asdf/fs 5. <ActionController::Routing::Route ":controller/:action/:id" || {:id=>nil, :action=>"index"}> failed because no controller found at subpath asdf/fs I don''t know how to catch this and perform the redirect. Am I doing something really stupid here? Any pointers would be greatly appreciated. Thanks in advance, andy -- Andrew Stone
Hello all, I just started learning rails last Friday and I think I''ve made some pretty good advancements in understanding Ruby/Rails. However I''ve run into an issue for which I cannot find the answer. I''ve been searching for a way to catch Routing errors. Here''s what I''m doing (trying to do) in routes.rb (what I added) map.connect '''', :controller => ''site'', :action => ''index'' map.connect '':client_name/'', :controller => ''site'', :action => ''index'' map.connect '':client_name/:controller/:action'' the site controller defines the actions and redirects for the client: def index redirect_to(:client_name => site.subdomain, :controller => ''home'', :action => ''index'') end this ends up with a pretty url of the type: www.mysite.com/client1/home/index everything is looking good, but I want to be able to catch routing errors and redirect them to the home page or show a nice 404 message redirect_to(:client_name => Site.find(1).subdomain, :controller => ''home'', :action = ''index'') But what I get when i type in a bogus url such as www.mysite.com/asdf/fs is the following: Routing Error No route for path: "asdf/fs" Failure reasons: 1. <ActionController::Routing::Route "" when {:controller=>"site", :action=>"index"}> failed because unused components were left: asdf/fs 2. <ActionController::Routing::Route ":client_name" when {:controller=>"site", :action=>"index"}> failed because unused components were left: fs 3. <ActionController::Routing::Route ":client_name/:controller/:action" || {:action=>"index"}> failed because no controller found at subpath fs 4. <ActionController::Routing::Route ":controller/service.wsdl" when {:action=>"wsdl"}> failed because no controller found at subpath asdf/fs 5. <ActionController::Routing::Route ":controller/:action/:id" || {:id=>nil, :action=>"index"}> failed because no controller found at subpath asdf/fs I don''t know how to catch this and perform the redirect. Am I doing something really stupid here? Any pointers would be greatly appreciated. Thanks in advance, andy -- Andrew Stone
Andrew Stone wrote:> I''ve been searching for a way to catch Routing errors. Here''s what > I''m doing (trying to do)[snip]> everything is looking good, but I want to be able to catch routing > errors and redirect them to the home page or show a nice 404 message > > redirect_to(:client_name => Site.find(1).subdomain, :controller => > ''home'', :action = ''index'') > > But what I get when i type in a bogus url such as > www.mysite.com/asdf/fs is the following: > > Routing Error >[snip]> I don''t know how to catch this and perform the redirect.A few thoughts: 1. See the wiki for instructions on dealing with errors in Rails in general and setting up dev to simulate production mode[1]. 2. Routing errors are not exposed in production mode. Rails renders the 404 found in /public, so you may be able to use that to suit your needs. 3. You can create a low-priority route that acts as a catch-all and renders to an error-handler controller. [Taking care of old links][2] by Ryan Carver explores this approach. [1] http://wiki.rubyonrails.com/rails/show/HowtoConfigureTheErrorPageForYourRailsApp [2] http://www.fivesevensix.com/articles/2005/04/18/taking-care-of-old-links hth, Lee
Thanks for the pointers Lee. To the list in general, sorry for the duplicate emails. Gmail didn''t show this in the rails inbox until a response was provided by Lee. I find that odd. Lesson learned. -andy On 5/13/05, Lee O''Mara <lee-O8glSrxzjJo@public.gmane.org> wrote:> Andrew Stone wrote: > > I''ve been searching for a way to catch Routing errors. Here''s what > > I''m doing (trying to do) > [snip] > > everything is looking good, but I want to be able to catch routing > > errors and redirect them to the home page or show a nice 404 message > > > > redirect_to(:client_name => Site.find(1).subdomain, :controller => > > ''home'', :action = ''index'') > > > > But what I get when i type in a bogus url such as > > www.mysite.com/asdf/fs is the following: > > > > Routing Error > > > [snip] > > I don''t know how to catch this and perform the redirect. > > A few thoughts: > > 1. See the wiki for instructions on dealing with errors in Rails in > general and setting up dev to simulate production mode[1]. > > 2. Routing errors are not exposed in production mode. Rails renders the > 404 found in /public, so you may be able to use that to suit your needs. > > 3. You can create a low-priority route that acts as a catch-all and > renders to an error-handler controller. [Taking care of old links][2] by > Ryan Carver explores this approach. > > [1] > http://wiki.rubyonrails.com/rails/show/HowtoConfigureTheErrorPageForYourRailsApp > > [2] http://www.fivesevensix.com/articles/2005/04/18/taking-care-of-old-links > > hth, > Lee > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Andrew Stone