gpt4 book ai didi

ruby-on-rails - 更改路由转换时如何重定向(301)?

转载 作者:行者123 更新时间:2023-12-04 06:06:34 26 4
gpt4 key购买 nike

我正在寻找一种在我的应用程序中进行重定向的简单方法。

情况:

我有这样的路线:

http://myapp.com/authors/5-hemingway/books/1-moby-dick

路由是这样翻译的(使用 gem 'i18n_routing'):

http://myapp.com/acutores/5-hemingway/libros/1-moby-dick

现在,我将 acutores 的翻译更改为 scriptores。简单的步骤,但我想将所有包含旧“acutores”资源名称的路由重定向到带有“scriptores”的路由。

我的猜测是,我应该在 routes.rb 中玩:

match "/acutores" => redirect("/scriptores")

但是如何在所有出现“acutores”的情况下有效地做到这一点? (尤其是嵌套路由)

最佳答案

这会将 /acutores/something 重定向到 /scriptores/something 但使用普通 /acutores 失败:

match "/acutores/*path" => redirect("/scriptores/%{path}")

这似乎可以同时处理:

match "/acutores(/*path)" => redirect {|params| "/scriptores/#{params[:path]}"}

--编辑

这将去掉所有尾部斜杠:

match "/acutores(/*path)" => redirect{ |params| "/scriptores/#{params[:path]}".chomp("/") }

浏览器缓存重定向有问题,所以修改后清空缓存。

关于ruby-on-rails - 更改路由转换时如何重定向(301)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4814063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com