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

后端 未结 4 1592
粉色の甜心
粉色の甜心 2021-02-07 03:50

I have a rails app (Rails 3.0) that I need to temporarily take out of service. While this is in effect, I want to create a new route that will direct all requests to a single pi

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 04:36

    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.

提交回复
热议问题