Ruby on Rails redirects how to pass parameters in a routes file

We recently changed the name of the product on our website from “bracelets” to “bracelets” and you need to keep the old routes for SEO purposes.

Essentially these routes

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

should switch to

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

I read the tutorial here http://guides.rubyonrails.org/routing.html#redirection and it looks like I will use the block syntax, but I'm still not sure how to follow the route correctly. I also want to know more about the route file, so any information would be great. thanks in advance

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

EDIT:

OK, I played with him a bit, and this is how it works with what I tried

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

Enter the URL: local: 3000 / products / bracelets / series-1 / small purple

Output URL: localhost: 3000 / products / wristbands
Error message: Invalid product: series-1
(So ​​the match worked, but the forwarding was not)

If I change the match to check for parameters like this:

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

I get the following:

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

So it looks like it does not recognize the second slash '/' or something like that. Any ideas?

+4
source share
2 answers

I was in the same situation and this works for me:

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

I transferred two blocks to a block.

+7
source

You can do this in Rails 5.

 match '/products/bracelets/:name/:name2', to: "/products/wristbands/%{name}/%{name2}" 
+1
source

Source: https://habr.com/ru/post/1439943/


All Articles