Marko Lekić
Marko Lekić

Reputation: 123

Handling non-existent routes in Rails 3.1 application

I just switched to rails not long ago and I'm loving it. Everything works well in my rails 3.1 application, but now at the end I want to somehow handle routes like www.myapp.com/something (off course, I dont have something controller). I get a routing error when I visit this page, but I was wandering if there was a way to do something about it, even if it's only redirecting these routes to my root_url. I tried to find the answer online with no luck.

Upvotes: 4

Views: 986

Answers (1)

Marian Theisen
Marian Theisen

Reputation: 6353

Yes, you can put a globbing route at the very end of your routes.rb to catch all misses:

match '/*paths', :to => 'some_controller#some_action'

in your Controller / action you can access the globbed path with

params[:paths]

more information http://guides.rubyonrails.org/routing.html#route-globbing

of course you can redirect without using an extra controller by using the redirect inline rack endpoint

match '/*paths' => redirect('/')

Upvotes: 7

Related Questions