【问题标题】:Redirection in Rails 4 routes在 Rails 中重定向 4 条路线
【发布时间】:2016-05-10 19:28:23
【问题描述】:

我的网站曾经在这里有移动视图:

https://www.example.com/m/home

我们已弃用移动视图,现在我需要一种简单的方法来修剪 URL 中的 /m/,以便请求继续到正确的页面。

例子:

https://www.example.com/m/about => https://www.example.com/about
https://www.example.com/m/user/:id => https://www.example.com/user/:id

我希望在 Rails 路由中解决这个问题,而不必引入新的控制器操作或干预 nginx。我有 100 多条路线。提前致谢。

Rails 版本:4.2

【问题讨论】:

    标签: ruby-on-rails routing


    【解决方案1】:

    有一个redirection module(也记录在the guide)。

    类似:

    get '/m/about', to: redirect('/about')
    get '/m/user/:id', to: redirect('/user/%{id}')
    

    您可以将其与 route globbing 结合使用以获得通用解决方案:

    get '/m/*path', to: redirect('/%{path}')
    

    【讨论】:

    • 我有大约 100 条路线,很遗憾,这是不可行的。谢谢!
    • @Brit200313 我刚刚添加了一个通用解决方案。
    【解决方案2】:

    稍微重构一下你的路线怎么样:

    例如:以前的 routes.rb

    resources :users
    # ...
    

    现在变成了:

    ['m', ''].each do |sc|
      scope sc do
        resources :users
        # ...
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2014-12-23
      • 2014-06-05
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多