Rails: redirect all unknown routes to root_url

后端 未结 5 1608
粉色の甜心
粉色の甜心 2020-11-30 23:02

Whenever a user hits the wrong page, rails shows 404.html from the public folder. However, I\'d like just to redirect the browser to the root page, without showing anything.

5条回答
  •  Happy的楠姐
    2020-11-30 23:50

    Rails 4-

    (routes.rb)

    You can still use a simple get to redirect all unknown routes.

      get '*path', to: 'home#index'
    

    If you wish to provide routing to both POST and GET requests you can still use match, but Rails wants you to specify the request method via via.

      match "*path" => "home#index", via: [:get, :post]  
    

    Remember that routes.rb is executed sequentially (matching the first route that fits the supplied path structure), so put wildcard catching at the bottom of your matchings.

提交回复
热议问题