Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将路径中的所有路由重定向到另一个路径,并保持URL的其余部分不变_Ruby On Rails_Ruby On Rails 4_Rails Routing - Fatal编程技术网

Ruby on rails 将路径中的所有路由重定向到另一个路径,并保持URL的其余部分不变

Ruby on rails 将路径中的所有路由重定向到另一个路径,并保持URL的其余部分不变,ruby-on-rails,ruby-on-rails-4,rails-routing,Ruby On Rails,Ruby On Rails 4,Rails Routing,我想重命名Rails应用程序中的资源,并将所有旧路由重定向到新路由,以向后兼容旧链接。例如,将“user”重命名为“member”,并将所有任意路由,如example.com/user/1/posts/all?order=date重定向到example.com/member/1/posts/all?order=date 如何设置routes.rb文件以将所有路径重定向到另一个路由,但在我要匹配的路径(包括url参数)之后维护所有路径参数 例如,它也应该适用于example.com/user/no

我想重命名Rails应用程序中的资源,并将所有旧路由重定向到新路由,以向后兼容旧链接。例如,将“user”重命名为“member”,并将所有任意路由,如
example.com/user/1/posts/all?order=date
重定向到
example.com/member/1/posts/all?order=date

如何设置
routes.rb
文件以将所有路径重定向到另一个路由,但在我要匹配的路径(包括url参数)之后维护所有路径参数

例如,它也应该适用于
example.com/user/no/real/path?foo=bar
,它应该重定向到
example.com/member/no/real/path?foo=bar

基本上,我想将任何
路径
重定向到
path.sub(/\A\/user/,'/member')

我签出了,但它似乎不适用于参数

这就是我目前的解决方案,非常糟糕:

get 'user/*path', to: redirect{|params, request|
    path = params[:path].sub('/user', '/member')
    params = request.params.except(:path).map{|k,v| "#{k}=#{v}"}.join('&')
    if params.presence
      "#{path}?#{params}"
    else
      path
    end
  }

您是否将原始路由设置为
resources:users
?我设置了,但它包含多个自定义成员和收集路由,因此无法用于解决问题您是否尝试此答案?是的,参数存在问题,它们没有传递到重定向路由。您尝试过其中任何一种吗,