Last Updated: February 25, 2016
·
1.168K
· 2called-chaos

Be careful with params.merge()

I've seen a lot of people doing it and I used to do it too, use params.merge(…) in link_to or redirect_to calls. You do this because you want to change one ore more parameters (like the locale, current page or search query) but leave the rest as it is.

But heads up! There is an issue with this solution:

link_to("Page 2", params.merge(page: 2)) # /users?page=2
link_to("parlez vous français?", params.merge(locale: "fr")) # /fr/about

What do you think would this request url do to the second link? Hint: You won't get a french customer anymore :)

http://localhost:3000/en/about?%68%6f%73%74=%77%77%77%2e%35%30%72%65%61%73%6f%6e%73%74%6f%68%61%74%65%74%68%65%66%72%65%6e%63%68%2e%63%6f%6d%2f%23&%70%6f%72%74=%38%30

equals

http://localhost:3000/en/about?host=www.50reasonstohatethefrench.com/%23&port=80

which results in you link target being

http://www.50reasonstohatethefrench.com/#/fr/about

There is also protocol you could tamper with. So if you have french customers you might want to know how to get out of this misery.

=> Read full article + possible solutions on our blog