nginx issue? redirect_to :back requires submitting the form twice for changes to be visible

0 views
Skip to first unread message

Daniel Nogues

unread,
May 1, 2008, 5:26:49 AM5/1/08
to Ruby Ireland
Hi,

I recently launched an application that has a localization feature.
The application changes locale submitting a PUT like this

http://domain.ie/accounts/change_locale/es

Then change_locale is a follows:

def change_locale
Locale.set params[:locale]
redirect_to :back
end

I am tailing production logs and don't see any errors, however I have
to submit twice my request for the change to be visible.
Maybe it is possible to address this issue changing nginx
configuration?

Regards,
Daniel

Dave Rice

unread,
May 1, 2008, 7:39:22 AM5/1/08
to ruby_i...@googlegroups.com
If I'm reading it right sounds like you have a session issue.

Is nginx proxying to a cluster of mongrels?
Have you setup a session store that can be shared between them

Best,
Dave

---
David Rice
+44 (0)78 708 12996




Daniel Nogues

unread,
May 3, 2008, 6:38:54 AM5/3/08
to Ruby Ireland
Hi Dave,

Thank you for your quick answer, caught me totally unexpected

I checked config/environment.rb, to see where the server side of
sessions get stored:

Rails::Initializer.run do |config|
config.action_controller.session_store = :active_record_store
end

They get stored in the database. Then I logged into the database and
deleted all records of table sessions.
Browse o the domain and a session gets created.

I run a mongrel cluster of two, so I stopped one. Then it worked!
I started the second mongrel, this time doesn't flip not even after
the second click.
I tail the log files and no differences except for time:

With 1 mongrel:

domain.ie:

Processing AccountsController#change_locale (for x.x.x.x at 2008-05-03
10:19:36) [PUT]
Session ID: xxx
Parameters: {"_method"=>"put", "action"=>"change_locale",
"controller"=>"accounts", "locale"=>"en"}
Redirected to http://domain.ie/
Completed in 0.00235 (425 reqs/sec) | DB: 0.00032 (13%) | 302 Found
[http://domain.i/accounts/change_locale/en]

domain.ie:

Processing HomeController#index (for x.x.x.x at 2008-05-03 10:19:36)
[GET]
Session ID: xxx
Parameters: {"action"=>"index", "controller"=>"home"}
Rendering within layouts/application
Rendering home/index
Completed in 0.48042 (2 reqs/sec) | Rendering: 0.42136 (87%) | DB:
0.04836 (10%) | 200 OK [http://domain.ie/]


With 2 mongrel:

domain.ie:

Processing AccountsController#change_locale (for x.x.x.x at 2008-05-03
10:20:16) [PUT]
Session ID: xxx
Parameters: {"_method"=>"put", "action"=>"change_locale",
"controller"=>"accounts", "locale"=>"en"}
Redirected to http://domain.ie/
Completed in 0.19691 (5 reqs/sec) | DB: 0.01202 (6%) | 302 Found
[http://domain.ie/accounts/change_locale/en]

domain.ie:

Processing HomeController#index (for x.x.x.x at 2008-05-03 10:20:17)
[GET]
Session ID: xxx
Parameters: {"action"=>"index", "controller"=>"home"}
Rendering within layouts/application
Rendering home/index
Completed in 0.09887 (10 reqs/sec) | Rendering: 0.07786 (78%) | DB:
0.01113 (11%) | 200 OK [http://domain.ie/]

You can see that when it fails,the time it takes to complete the home
request is 5 times lower: 0.09887 versus 0.48042; also the PUT takes
100 times higher: 0.19691 versus 0.00235
Does this tells you something?

Dan


Daniel Nogues

unread,
May 6, 2008, 6:18:41 AM5/6/08
to Ruby Ireland
Hi,

I moved from nginx to apache
Despite having more consistency, issues still arise:

1. Keep bouncing from spanish to english, short after, the page
doesn't refresh any longer to the correct language.
2. Switch language and change tab (like from Home to Members),
sometimes the language bounces back to the previous one.

The site is spanglishfun.com. Despite it is not yet in production you
can verify these issues there.
Our production site at the moment is spanglish.ie, which exists for
english only.

Regards,
Daniel

John Ward

unread,
May 6, 2008, 7:14:24 PM5/6/08
to ruby_i...@googlegroups.com
In your previous code you were setting Locale.set, I'm not 100% familar but it looks like you are setting the Locale for the request, or the running process but nothing more.

You should store the locale in the session, and then have a before_filter that uses it on subsequent requests to set the locale.

 def change_locale
       session[:locale] = params[:locale]

       Locale.set params[:locale]
       redirect_to :back
 end

#before_filter code in application.rb

before_filter :set_locale_from_session

def set_locale_from_session
  Locale.set session[:locale] if session[:locale]
end

Daniel Nogues

unread,
May 7, 2008, 8:40:27 AM5/7/08
to Ruby Ireland
Thank you John,

As much as I try to make it fail, it doesn't.

In my code I had

def set_locale_from_session
Locale.set(Locale.language ? Locale.language.code : 'en')
true
end

I didn't realize that "Locale.language" was sitting in memory, not the
database. By using sessions instead, the issue Dave mentioned before
gets fixed

Best regards,
Daniel
Reply all
Reply to author
Forward
0 new messages