Iván Vega Rivera
2005-Nov-18 23:06 UTC
How to create an object in environment.rb and use it to share data between controllers
Hi, I was using a global ($) variable for this, but it seems global variables remain loaded all the time that webrick is running, and I need a fresh object for each HTTP request. So I was trying to use the request.env array to add my own data there, but it seems that that object isn''t created in environment.rb, so I cannot use that approach. I need to create an object with some default values on environment.rb, and then each controller can override any of its attributes. Could you please help me out? - Ivan V.
randy marmer
2005-Nov-19 00:39 UTC
Re: How to create an object in environment.rb and use it to
ivanvega wrote:> Hi, > > I was using a global ($) variable for this, but it seems global > variables remain loaded all the time that webrick is running, and I need > a fresh object for each HTTP request. > > So I was trying to use the request.env array to add my own data there, > but it seems that that object isn''t created in environment.rb, so I > cannot use that approach. I need to create an object with some default > values on environment.rb, and then each controller can override any of > its attributes. > > Could you please help me out? > > - Ivan V.Hey Ivan, As newby-on-rails, I don''t know if this suggestion will be of any help to you, but for what it''s worth... Because I will soon need to figure out how to do what is commonly referred to as an "application variable" in other languages... (i.e. a variable that all open sessions have access to). I started doing some research & ran across something called "PStore". It''s described as a way of storing session information in a file in the tmp dir... I know this is not what you are trying to do, but since it''s possible, I assume, to read & write to this file from anywhere in the app (I think read & write locking is built-in), perhaps you could use it for your purposes? I haven''t figured out how to use it yet, but here''s a wiki entry that mentions it as well as other approaches; http://wiki.rubyonrails.com/rails/pages/HowtoChangeSessionStore/ I hope you get a more useful answer from someone who knows far more than I... Randy -- Posted via http://www.ruby-forum.com/.
Iván Vega Rivera
2005-Nov-19 00:57 UTC
Re: Re: How to create an object in environment.rb and use it to
Hey, Thanks for the input. Unfortunately, as you point out, that''s not what I''m trying to do, because I need a clean object variable each time a page is requested, so a session/file wouldn''t work (well it could, but it would be very complicated and way overkill). But I''m thinking that might be of use for another area of my project! I believe this is only an issue with webrick, because the application is running the whole time, so I suppose Apache wouldn''t have this problem because it runs the script from scratch for each request, I think... Let''s hope someone can help me out! Ivan V. randy marmer wrote:> ivanvega wrote: > >> Hi, >> >> I was using a global ($) variable for this, but it seems global >> variables remain loaded all the time that webrick is running, and I need >> a fresh object for each HTTP request. >> >> So I was trying to use the request.env array to add my own data there, >> but it seems that that object isn''t created in environment.rb, so I >> cannot use that approach. I need to create an object with some default >> values on environment.rb, and then each controller can override any of >> its attributes. >> >> Could you please help me out? >> >> - Ivan V. >> > > Hey Ivan, > > As newby-on-rails, I don''t know if this suggestion will be of any help > to you, but for what it''s worth... > > Because I will soon need to figure out how to do what is commonly > referred to as an "application variable" in other languages... (i.e. a > variable that all open sessions have access to). > > I started doing some research & ran across something called "PStore". > > It''s described as a way of storing session information in a file in the > tmp dir... I know this is not what you are trying to do, but since it''s > possible, I assume, to read & write to this file from anywhere in the > app (I think read & write locking is built-in), perhaps you could use it > for your purposes? > > I haven''t figured out how to use it yet, but here''s a wiki entry that > mentions it as well as other approaches; > > http://wiki.rubyonrails.com/rails/pages/HowtoChangeSessionStore/ > > I hope you get a more useful answer from someone who knows far more than > I... > > Randy > >
Ezra Zygmuntowicz
2005-Nov-19 01:06 UTC
Re: Re: How to create an object in environment.rb and use it to
Ivan- You can place a before filter in your application.rb that gets initialized to whatever you want on each request. Like htis: class ApplicationController < ActionController::Base before_filter :setup_foo attr_reader :foo attr_writer :foo def setup_foo @foo = {:a => ''foo, :b => ''bar'', :c => ''qux''} end end Now @foo will be available to all your controllers and views. And the attr_reader and attr_writer will set it up so your controllers can modify foo per request. But @foo will be reset on each request. I think this is what you are trying to do. Let me know if you need more help. Cheers- -Ezra On Nov 18, 2005, at 4:57 PM, Iván Vega Rivera wrote:> Hey, > > Thanks for the input. Unfortunately, as you point out, that''s not > what I''m trying to do, because I need a clean object variable each > time a page is requested, so a session/file wouldn''t work (well it > could, but it would be very complicated and way overkill). But I''m > thinking that might be of use for another area of my project! > > I believe this is only an issue with webrick, because the > application is running the whole time, so I suppose Apache wouldn''t > have this problem because it runs the script from scratch for each > request, I think... > > Let''s hope someone can help me out! > > Ivan V. > > randy marmer wrote: >> ivanvega wrote: >> >>> Hi, >>> >>> I was using a global ($) variable for this, but it seems global >>> variables remain loaded all the time that webrick is running, and >>> I need >>> a fresh object for each HTTP request. >>> >>> So I was trying to use the request.env array to add my own data >>> there, >>> but it seems that that object isn''t created in environment.rb, so I >>> cannot use that approach. I need to create an object with some >>> default >>> values on environment.rb, and then each controller can override >>> any of >>> its attributes. >>> >>> Could you please help me out? >>> >>> - Ivan V. >>> >> >> Hey Ivan, >> >> As newby-on-rails, I don''t know if this suggestion will be of any >> help to you, but for what it''s worth... >> >> Because I will soon need to figure out how to do what is commonly >> referred to as an "application variable" in other languages... >> (i.e. a variable that all open sessions have access to). >> >> I started doing some research & ran across something called "PStore". >> >> It''s described as a way of storing session information in a file >> in the tmp dir... I know this is not what you are trying to do, >> but since it''s possible, I assume, to read & write to this file >> from anywhere in the app (I think read & write locking is built- >> in), perhaps you could use it for your purposes? >> >> I haven''t figured out how to use it yet, but here''s a wiki entry >> that mentions it as well as other approaches; >> >> http://wiki.rubyonrails.com/rails/pages/HowtoChangeSessionStore/ >> >> I hope you get a more useful answer from someone who knows far >> more than I... >> >> Randy >> >> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Iván Vega Rivera
2005-Nov-21 18:11 UTC
Re: Re: How to create an object in environment.rb and use it to
Ezra Zygmuntowicz wrote:> Ivan- > > You can place a before filter in your application.rb that gets > initialized to whatever you want on each request. Like htis: > > class ApplicationController < ActionController::Base > > before_filter :setup_foo > > attr_reader :foo > attr_writer :foo > def setup_foo > @foo = {:a => ''foo, :b => ''bar'', :c => ''qux''} > end > > end > > > Now @foo will be available to all your controllers and views. And > the attr_reader and attr_writer will set it up so your controllers can > modify foo per request. But @foo will be reset on each request. I > think this is what you are trying to do. Let me know if you need more > help. > > Cheers- > > -Ezra >Thanks a lot Ezra, that''s just what I was looking for. Ivan V.