Ahmed Elkoussy
Ahmed Elkoussy

Reputation: 8588

How to keep the query params when changing locale in Rails 6

I am trying to preserve query params when changing locale in Rails 6, I only found the default_url_options method & if I try to pass the params to that it doesn't work throwing unpermitted params passed exception

For example:

A URL having http://localhost:3000/some_path?token=ABcasdjlaweQWd

Should be changed to http://localhost:3000/de/some_path?token=ABcasdjlaweQWd

But currently, it only becomes: http://localhost:3000/some_path & I lose the query params

Keeping things secure is important, so I don't want to hack my way disabling the params filter, but it should be possible to pass the params during language change without big hacks.

Update:

I am changing local by navbar link_to btn (for different locale), for example: link_to url_for( :locale => 'fr' ) that will change locale part of the current url to the french locale for example

What is the correct way to maintain the query params on language change?

Upvotes: 0

Views: 1063

Answers (1)

Ahmed Elkoussy
Ahmed Elkoussy

Reputation: 8588

Without overriding the default_url_options, a better way is to make the language change button behave differently, so that it won't remove the query params

So instead of this:

= link_to url_for( locale: 'en' )

It should be changed to this, which will keep the params:

 = link_to request.params.merge( locale: 'en' )

Upvotes: 3

Related Questions