Rails: changing locale and keeping page parameters

拟墨画扇 提交于 2019-12-05 05:02:54
Chris Salzberg

EDIT: THIS IS NOT A GOOD WAY TO DO THIS. See comments below.

The problem is that you are not passing the current params to link_to when you create the locale switcher link.

Change your navbar link to:

<%= link_to t('header.english'), params.merge(locale: "en") %>

See also: Add querystring parameters to link_to

If you need locale param to stay in all requests it's good to use this approach:

# app/controllers/application_controller.rb
def default_url_options(options={})
  { locale: I18n.locale }
end

from rails guide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!