【问题标题】:How to redirect Rails old path to the new path?如何将 Rails 旧路径重定向到新路径?
【发布时间】:2018-02-24 20:48:12
【问题描述】:

所以这是我所有用户的路径:

http://localhost:3000/users

我想将 /users 更改为 /members ,所以我这样做了:

get '/members' => 'users#index', as: "members"

这样,人们可以访问 http://localhost:3000/members 并在 /users 中获得所有相同的内容。

但问题是 http://localhost:3000/users 仍然可供人们访问。如何删除/隐藏/重定向 /users,以便人们在尝试使用旧的 /users 网址时看到 /membershttp://localhost:3000/users ?

【问题讨论】:

    标签: ruby-on-rails routes ruby-on-rails-5


    【解决方案1】:

    嗯,这取决于你是想消除路径还是简单地覆盖它

    既然你提到http://localhost:3000/users 仍然映射到某个地方,我将假设你有get '/users' => 'users#index' 或者更有可能像resources :users 这样的行。如果您有第一行,只需将其删除,如果是第二行,您可以使用 :except 选项限制您创建的路由

    resources :users, except: :index

    对于重定向,hamdi 发布的解决方案也应该可以工作。

    查看 Rails 路由页面了解更多信息:http://guides.rubyonrails.org/routing.html

    干杯

    【讨论】:

      【解决方案2】:

      你可以像下面这样重定向:

      get '/users', to: redirect('/members')
      

      您还可以在以下链接中找到更多关于重定向的信息:

      http://guides.rubyonrails.org/routing.html#redirection

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-16
        • 1970-01-01
        • 2014-09-12
        • 1970-01-01
        • 2014-04-27
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多