Rails - how to handle routes that don't exist

2019-05-06 20:38发布

问题:

My route looks like

match 'about' => 'company#about'

When I set to the url http://localhost:3000/aboutttt, I get the error message

No route matches [GET] "/aboutttt"

I want to give the user better feedback than that.

The best solution by my opinion could be redirect the app back, or on the homepage of the app, but exist in routes any way to set default route, when I get the error above?

回答1:

Run your app with the environment set to production and I think you'll find you'll no longer see this message. It's a convenience for debugging while you're developing.

There's a setting in config/environments/production.rb or config/environments/development.rb (I can't remember which way round) which controls whether exceptions are rendered as views. This is off for production - instead you get the 404.html that's in public, which you can change as you please.



回答2:

While you are on development. Make changes inside config/environments/development.rb by setting: config.consider_all_requests_local = false

This will disable full error reporting and instead shows a page with interactive response for users. But this is already disabled on production environment. But I don't recommend turning it off in development mode.