I’m assuming by global that you want a variable to exist in all
controllers and views. While using sessions to accomplish this works,
one ends up having duplicate code to access these sessions, resulting
in a coupled unmanageable controller/view set. I found that the best
solution for “globals” is to define the applicationController’s
initialize method and create the “global” data as an instance
variable. For example, to share a user model across all views and
controllers code the following:
class ApplicationController < ActionController::Base
@user = session[:user] ||= User.find_by_username(‘nobody’, :include
where session[:user] exists or the default ‘nobody’ user is created
and assigned to both @user and session[:user].
so far I have found nothing wrong with defining the initializer.
I am not responsible for any “damages” resulting from the advice that
I give. I reserve the right to change my mind and be victim to any
shift in circumstances.