what should i be using instead of a global variable in my rails3 app?
I have a rails 3 app that is using sub domains for each account.
When a开发者_开发百科 visitor visits a subdomain eg acme.domain.com, i lookup the subdomain from the database using the request.subdomain. I then store the subdomain id in a glabla variable.
I do this as i need access to this variable from controller, views and models so i can scope everything to just this subdomain. eg just users, companies, pages etc for this subdomain.
From what i hear using globals is bad design and can be buggy etc, but what should i use instead in this case ?
please can anyone help ?
best regards rick
Use sessions or cookies for this.
Global variables are accessible for all users, while sessions and cookies are personalized
http://guides.rubyonrails.org/security.html#sessions
Obviously your controller knows about the subdomain. Your models and views shouldn't need to lookup this global at all, because this builds am artificial dependency into them, reducing reusability and testability.
If they really need additional data to work, and you can't keep that in the controller's Model.find() for example, the controller shall pass this data to everything else explicitly.
精彩评论