问题
So I have two heroku apps:
- http://production-app.com (mapped from http://production-app.heroku.com)
- http://development-app.heroku.com
On production-app.com
, I have several subdomains using the Heroku custom domains addon with Zerigo (not the wildcard domain addon):
- http://blog.production-app.com
- http://api.production-app.com
On development-app.heroku.com
, I also have those custom subdomains, but since I don't have a custom domain, I just use the wildcard addon.
In my routes.rb
, using Subdomain-Fu, I have the subdomains working locally and on both Heroku apps.
The issue I'm facing now is, how do I keep the session in sync between all the subdomains?
I have tried adding this to production.rb
:
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(:session_domain => '.production-app.com')
And even combinations of this:
begin
config.action_controller.session[:domain] = '.production-app.com'
config.action_controller.session[:session_domain] = '.production-app.com'
rescue
config.action_controller.session = {:domain => '.production-app.com', :session_domain => ".production-app.com"}
end
...and for the dev site, both of these:
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(:session_domain => '.heroku.com')
# or
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(:session_domain => '.development-app.heroku.com')
None of those keep the session between subdomains. How do I get this working when I have a custom domain and when I'm just running off a Heroku subdomain?
Thanks!
回答1:
In your config/initializers/session_store.rb, tell rails to set the cookie across all domains:
Rails.application.config.session_store :cookie_store, :key => '_yourapp_session', :domain=>:all
Same as this SO question
If you are wanting to persist your session between "development-app.heroku.com" and "production-app.com" that cannot be done unless those are the same codebase. If they are the same codebase, then use a before_filter on your application controller to redirect to production-app.com
回答2:
On development-app.heroku.com, I also have those custom subdomains, but since I don't have a custom domain, I just use the wildcard addon.
Is this addon still available for using custom subdomains like api.mydevdomain.herokuapp.com
?
来源:https://stackoverflow.com/questions/5340989/subdomain-session-not-working-in-rails-2-3-and-rails-3-on-heroku-with-without-a