Skip to main content

Error Handling in Ruby 4+

Rails • Asked by Nathan La Barbera

Does anyone have any thoughts or why this might be a bad practice?

I am using the following code as the last route within my routes.rb
get "*path", to: redirect('/')

It seems to handle very well if someone navigates to a URL that isn't valid rather then displaying the default Rails "Routing Error".

I am wanting to know if there is a better method and if this is standard practice.

Thanks everyone.


I don't think I would consider that being a bad practice. It's going to be nice to have invalid url's redirected to the root. Potentially this is something that Google frowns upon because you're not returning a 404 response though. That might have negative SEO impact potentially.

The other thing this wouldn't handle is missing records. Imagine you have blog posts at /posts/:id. If you visit /posts/asldfjalsdf and a post with that ID doesn't exist, you'd get an error. There's a method called rescue_from that you can use in the controllers to catch from issues like that one. That might help if you want to have a more robust invalid url catching.

I'd say do some research to see if Google doesn't like redirecting to root for missing pages and see if that's a bad thing or not. Maybe someone else here knows too.


Redirecting to root path on a 404 request is not standard practice. And it will impact your SEO in a negative fashion although not to an extreme. 404s need to be 404s.


Perfect, great advice guys! Thank you for the feedback! I wanted to use the rescue_from method but couldn't get it to work. I'll look more into it and spend more time on it. Thanks again guys.


Cool deal, ultimately it's up to you how you handle 404s but if your UI is tight you should never receive a 404 in your app from clicking on something unless it's broken. Someone randomly doing a GET request on a URL that doesn't exist should be met with a 404 to keep in compliance with best practices. Ultimately, you can handle it any way you like but I personally like to stick with standards whenever possible.

Cheers!


If we're talking about best practice I usually do the following. First, in the routes file I set a wildcard route to a method in the application controller. (get '*unmatched_route', to: 'application#raise_not_found'). Then in the application controller I set rescues for managing errors to my static error pages.

rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from Exception, with: :not_found
rescue_from ActionController::RoutingError, with: :not_found

def raise_not_found
raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")
end

def not_found
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end

def error
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/500", :layout => false, :status => :error }
format.xml { head :not_found }
format.any { head :not_found }
end
end


Jeff, this is actually a clean way to handle it. Thanks for chiming in.


Jeff, thank you for the feedback. This worked perfectly and achieved everything I needed. Thank you again.


No problem - glad it helped.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.