Ruby on Rails重定向如何在路由文件中传递参数

Ruby on Rails重定向如何在路由文件中传递参数

问题描述:

我们最近在我们的网站上将产品名称从手镯"更改为腕带",并且出于SEO目的,需要保留旧路线.

We recently changed the product name on our website from 'bracelets' to 'wristbands' and need to keep the old routes around for SEO purposes.

基本上,这些路线

www.hostname.com/products/bracelets/series-1/
www.hostname.com/products/bracelets/series-1/small-purple

www.hostname.com/products/bracelets/series-1/
www.hostname.com/products/bracelets/series-1/small-purple

应路线到

www.hostname.com/products/wristbands/series-1/
www.hostname.com/products/wristbands/series-1/small-purple

www.hostname.com/products/wristbands/series-1/
www.hostname.com/products/wristbands/series-1/small-purple

我正在此处阅读本教程 http://guides.rubyonrails.org/routing.html#重定向,看起来我将使用块语法,但仍不确定如何正确进行路由.我也希望了解有关路由文件的更多信息,因此任何信息都将非常有用.预先感谢

I am reading the tutorial here http://guides.rubyonrails.org/routing.html#redirection and looks like i'll be using the block syntax, but am still not sure how to do the route properly. I'm looking to learn more about the routes file as well, so any information would be great. Thanks in advance

match "/bracelets/:name" => redirect {|params| "/wristbands/#{params[:name]}" }

好的,我已经玩了一段时间,这就是它与我尝试过的工作方式

OK i've been playing with it for a bit, and here is how it is working with what I have tried

match "/products/bracelets/:name/:name2" => redirect {|params| "/products/wristbands/#{params[:name].pluralize}/#{params[:name2].pluralize}" }

输入网址: localhost:3000/products/bracelets/series-1/small-purple

输出网址: localhost:3000/products/wristbands
错误消息:无效的产品:series-1
(因此匹配有效,但重定向无效)

Output URL: localhost:3000/products/wristbands
Error Message: Invalid Product: series-1
(So the match worked, but the redirect didn't)

如果我更改匹配以检查这样的参数:

If I change the match to inspect params like this:

match "/products/balance-bracelets/:name/:name2" => redirect {|params| "/products/wristbands/#{params.inspect}" }

我得到以下信息:

 /products/wristbands/{:name=>"series-1", :name2=>"small-purple"}

因此,似乎无法识别第二个斜杠"/"或其他内容.有任何想法吗?

So it appears it isn't recognizing the second slash '/' or something. Any Ideas?

我处在相同的情况下,并且对我有用:

I'm was at the same situation and that works for me:

match "/products/bracelets/:name/:name2" => redirect {|params, request| "/products/wristbands/#{params[:name].pluralize}/#{params[:name2].pluralize}" }

我已将两个点数添加到该区块中.

I've passed two agruments into the block.