0

I have a problem when I try to change the locale on my webapp. In dev mode (webrick) all is ok, but when I deploy on my prd env (Unicorn + NGINX) a strange behaviour happens:

For each flag i set this link

   <%= link_to "<button class='btn flag flag-#{lang}'></button>".html_safe, {controller: 'amministratore/localecontroller', action: 'set', locale: lang} %>

where lang is the locale.

Inside the controller I have this simple logic:

def set
    I18n.locale = params[:locale] || I18n.default_locale
    redirect_to amministratore_items_url
end

The problem is this: When I click on the flags (on dev all is ok), the page is not reloaded but the locale is changed (checked with <%=debug(params)%>. This mean that the other urls inside the page (with default_url_option that set the current locale to all urls) have not the correct locale.

Example

  1. Current state : (url in page) https://stackoverflow.com/questions/ask?locale=it - I18n.locale = "it"
  2. Click on flag EN
  3. Updated state : (url in page) https://stackoverflow.com/questions/ask?locale=it - I18n.local = "en"

The same requests between prd and dev are equals (headers, etc, etc)

Honestly I do not know where to look the solution...

Thanks

Community
  • 1
  • 1
Luis C.
  • 727
  • 3
  • 22
  • 34
  • You use Rails 4? Read about turbolinks. – Mike Szyndel May 21 '14 at 08:47
  • 1
    Although out of topic, I strongly suggest you only use English in your code, especially when naming things (I refer to `amministratore_items_url`). You never know when a non-Italian speaker will need to contribute or maintain your code :) – Kostas Rousis May 21 '14 at 08:49
  • @rkon Thanks for suggestion. Infact the pluralize of italian terms is not a really good thing. – Luis C. May 21 '14 at 08:59

1 Answers1

0

I guess you're using Rails 4, so this is Turbolinks issue. I'm not gonna go into details (you can read about them on the gem page) but solution is adding data-no-turbolink attribute to your button.

Additionally I don't get why you put button inside a link instead of using button_to Rails helper?

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63