2011-01-27 52 views
3

我正在尋找一種簡單的方法在我的應用程序中進行重定向。如何在更改路由轉換時重定向(301)?

狀況:

我有這樣的路線:

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

的路線被轉換這種方式(使用寶石 'i18n_routing'):

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

現在,我改變了翻譯acores的用法和樣例:簡單的步驟,但我想重新將所有包含舊的「acutores」資源名稱的路線重定向到帶有「scriptores」的路線。

我的猜測是,我應該在routes.rb中玩:

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

但如何對其中「acutores」出現在所有情況下,有效地做到這一點? (尤其是嵌套的路線)

回答

8

這重定向/acutores/something/scriptores/something但無法用普通/acutores

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

這似乎同時處理:

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

--edit

這將擺脫所有尾隨的斜線:

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

我曾與瀏覽器緩存重定向問題,因此清空後修改的高速緩存。

+0

謝謝,看起來不錯,但我不確定我是否想在重定向後有一個斜槓。在你的解決方案中,「/ acutores」轉到「/ scriptores /」。如何去除最後一個「/」? – 2011-01-30 19:03:16