Tuesday, January 27, 2009

Subdomains and Rails

I was trying to setup the proper environment settings in Rails 2 to support cross-subdomain cookie sharing (so the user doesn’t have to login again if they manage multiple subdomain accounts). I was experiencing some inconsistencies when trying to make sure it was working. Here is a tip:

Clear your cookies (the Firefox Web Developer plug-in is great for viewing/deleting cookies being assigned) before testing this! If you have any old cookies from an old login session before you converted to subdomains, you’ll experience some oddities that will make you think it isn’t working.

BTW, if you are using the Ruby on Rails Wiki page for customizing your session or cookie settings, you’ll notice that the wiki provides multiple ways to change the session domain. This is the line that worked for me to customize the domain using Rails 2.0.2:

ActionController::Base.session_options[:session_domain] = ‘.mydomain.com’

Using constants in shared config file

Constants of rails application can be used in shared config file instead of using it in different development.rb and production.rb for different environments.
You can create ruby config file "shared.rb" in "config/environments/" folder of your rails application.
Then just add one statement in "config/environment.rb" file to use shared file throughout the application as following:

require 'environments/shared'