All threads / Error Handling in Ruby 4+
Ask A Question


You’re not receiving notifications from this thread.

Error Handling in Ruby 4+

Nathan La Barbera asked in Rails

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.


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"No route matches #{params[:unmatched_route]}")

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 }

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 }

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.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 70,078+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.