How can I create a Rails 3 route that will match all requests and direct to one resource / page?

瘦欲@ 提交于 2019-12-03 05:50:16

Rails needs to bind the url parameters to a variable, try this:

match '*foo' => 'content#holding'

If you also want to match /, use parenthesis to specify that foo is optional:

match '(*foo)' => 'content#holding'

I ran into something like this where I had domain names as a parameter in my route:

match '/:domain_name/', :to => 'sitedetails#index', :domain_name => /.*/, :as =>'sitedetails'

The key piece to this was the /.*/ which was a wildcard for pretty much anything. So maybe you could do something like:

match '/:path/', :to => 'content#holding', :path=> /.*/, :as =>'whatever_you_want'

I did this just yesterday and first came up with the solution that klochner shows. What I didn't like about this is the fact that whatever you enter in the URL, stays there after the page loads, and since I wanted a catch all route that redirects to my root_url, that wasn't very appealing.

What I came up with looks like this:

# in routes.rb
get '*ignore_me' => 'site#unknown_url'

# in SiteController
def unknown_url
  redirect_to root_url
end

Remember to stick the routes entry at the very bottom of the file!

EDIT: As Nick pointed out, you can also do the redirect directly in the routes file.

Sergio Tulentsev

Where in "routes.rb" is this line located?

To have priority over other routes, it has to be placed first.

As an alternative, you can look into this: http://onehub.com/blog/posts/rails-maintenance-pages-done-right/

Or this: Rails: admin-only maintenance mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!