redirect_to :back
do not accept any additional params, as it will use the url supplied by the request headers. You should probably do:
url_hash = Rails.application.routes.recognize_path URI(request.referer).path
url_hash[:locale] = params[:set_locale]
redirect_to url_hash
Explanation:
Rails.application.routes.recognize_path URI(request.referer).path
This line takes the referer url (this is the url the request comes from and it is used when you call redirect_to :back
). Then it is parsed with URI to extract the path alone, like /de/faq
.
Having the path we are using Rails plumbing function recognize_path
. This method uses your routes to translate given path into a hash like {controller: 'MyController', action: 'my_action', param: 'my_param', ... }
. Since your route includes locale in the URL, the locale used in referrer URL will be accessible in this hash under :locale
key.
Now the second line is just to change the value of :locale
in this hash to whatever value of locale has been passed. Then if you do redirect with this hash, rails will use this hash to generate the URL from it, which will be exactly same as referer URL except for locale.
Most likely you still need to use the session to store the locale.