Hi: Sorry if this is a painfully stupid question... I have some data I need through the life of someone''s session. In the application controller, I grab the data and store it like so: session[:foo] = @foo Now, whenever I need to access data about foo I don''t need to cause any DB io, I can just grab foo from the session (it''s very small fyi). Here''s what I have: # Instantiate Foo @foo = Foo.new() @foo = session[:foo] # Grab Bar with Foo @bar = @foo.bar Is this the bestest Rails way of handling this? Is there a more concise way? -- Posted via http://www.ruby-forum.com/.
You can make yourself some methods in application_controller.rb to have the methods available everywhere, or in a particular controller/helper if that''s what you need. To get the foo in the session (or new if there isn''t a foo in the session) def foo session[:foo] ? session[:foo] : Foo.new end To set the session foo def foo=(a_foo) session[:foo] = a_foo end then call foo.bar On 5/12/06, Joe Cairns <joe.cairns@gmail.com> wrote:> > Hi: Sorry if this is a painfully stupid question... > > I have some data I need through the life of someone''s session. In the > application controller, I grab the data and store it like so: > session[:foo] = @foo > > Now, whenever I need to access data about foo I don''t need to cause any > DB io, I can just grab foo from the session (it''s very small fyi). > > Here''s what I have: > # Instantiate Foo > @foo = Foo.new() > @foo = session[:foo] > > # Grab Bar with Foo > @bar = @foo.bar > > Is this the bestest Rails way of handling this? Is there a more concise > way? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060511/9d6389d0/attachment.html
I searched Google for this* but didn''t find anything about it from casual browsing! Is there an official way to delete controllers/views, or can I just delete folders? *http://www.google.com/search?hl=en&q=delete+view+controller+ruby+on+rails&btnG=Google+Search
./script/destroy controller_name On 5/11/06, c.k.lester <rails@cklester.com> wrote:> I searched Google for this* but didn''t find anything about it from > casual browsing! Is there an official way to delete controllers/views, > or can I just delete folders? > > *http://www.google.com/search?hl=en&q=delete+view+controller+ruby+on+rails&btnG=Google+Search > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein 303-947-0446 http://www.benr75.com
There''s nothing wrong with just deleting files - although you might not get all of them. There is a destroy script you can use: ruby script/destroy controller ThingController or ruby script/destroy scaffold Thing Steve c.k.lester wrote:> I searched Google for this* but didn''t find anything about it from > casual browsing! Is there an official way to delete controllers/views, > or can I just delete folders? > > *http://www.google.com/search?hl=en&q=delete+view+controller+ruby+on+rails&btnG=Google+Search > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Daniel ----- wrote:> You can make yourself some methods in application_controller.rb to have > the > methods available everywhere, or in a particular controller/helper if > that''s > what you need. > > > To get the foo in the session (or new if there isn''t a foo in the > session) > def foo > session[:foo] ? session[:foo] : Foo.new > end > > To set the session foo > def foo=(a_foo) > session[:foo] = a_foo > end > > then call > foo.barOk I implemented that and it works for the first action, and then this hits: Application error Change this error message for exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code) in public/500.html This was in the log file: Session contains objects whose class definition isn''t available. Remember to require the classes for all objects kept in the session. (Original exception: uninitialized constant Generic [NameError]) ./script/../config/../vendor/rails/actionpack/lib/action_controller/cgi_process.rb:147:in `stale_session_check!'' ./script/../config/../vendor/rails/actionpack/lib/action_controller/cgi_process.rb:107:in `session'' ./script/../config/../vendor/rails/actionpack/lib/action_controller/base.rb:887:in `assign_shortcuts_without_flash'' ./script/../config/../vendor/rails/actionpack/lib/action_controller/flash.rb:141:in `assign_shortcuts'' ./script/../config/../vendor/rails/actionpack/lib/action_controller/base.rb:375:in `process_without_filters'' ./script/../config/../vendor/rails/actionpack/lib/action_controller/filters.rb:377:in `process_without_session_management_support'' ./script/../config/../vendor/rails/actionpack/lib/action_controller/session_management.rb:117:in `process'' ./script/../config/../vendor/rails/railties/lib/dispatcher.rb:38:in `dispatch'' ./script/../config/../vendor/rails/railties/lib/webrick_server.rb:115:in `handle_dispatch'' ./script/../config/../vendor/rails/railties/lib/webrick_server.rb:81:in `service'' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'' c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'' c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' ./script/../config/../vendor/rails/railties/lib/webrick_server.rb:67:in `dispatch'' ./script/../config/../vendor/rails/railties/lib/commands/servers/webrick.rb:59 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require__'' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'' ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:147:in `require'' ./script/../config/../vendor/rails/railties/lib/commands/server.rb:30 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require__'' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require'' script/server:3 -- Posted via http://www.ruby-forum.com/.
I did some research and am coming up small on the error: "Session contains objects whose class definition isn''t available. Remember to require the classes for all objects kept in the session. (Original exception: uninitialized constant Generic [NameError])" I added "require foo.rb" to all of my controllers, but this error persists. I''ve also tried require_dependencies. This error creeps up on the second time a controller is called. The cause of this problem is when I reference an object in the session like so: @foo= foo.new() @foo = session[:foo] @bar = @foo.bar Or even when I use the method a previous poster mentioned, like so: def foo session[:foo] ? session[:foo] : Foo.new end def foo=(a_foo) session[:foo] = a_foo end foo.bar If I do this: @foo = Foo.find( session[:site][:id] ) ...the problem stops. The trouble is this defeats my purpose of putting the object into the session in the first place. Any ideas would be great, I''m at the end of my rope on this one. -- Posted via http://www.ruby-forum.com/.
Following up on this, does this delete the associated tests as well and other items that are created when you do script/generate controller myController? Also, it would be nice to have a script/rename controller OrigName NewName I know I''ve screwed up more than once naming my stuff the Rails way ;) On 5/11/06, Stephen Bartholomew <sb@2404.co.uk> wrote:> > There''s nothing wrong with just deleting files - although you might not > get all of them. There is a destroy script you can use: > > ruby script/destroy controller ThingController > > or > > ruby script/destroy scaffold Thing > > > Steve > > > c.k.lester wrote: > > I searched Google for this* but didn''t find anything about it from > > casual browsing! Is there an official way to delete controllers/views, > > or can I just delete folders? > > > > * > http://www.google.com/search?hl=en&q=delete+view+controller+ruby+on+rails&btnG=Google+Search > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ryan Prins rprins@gmail.com http://www.lazyi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060511/c06fd6b8/attachment.html
> Following up on this, does this delete the associated tests as well> and other items that are created when you do script/generate > controller myController? As far as i''m aware it does, yeah. > I know I''ve screwed up more than once naming my stuff the Rails way ;) Not using the ''Rails'' way? I sense the darkside in you... :0) Steve Ryan Prins wrote:> Following up on this, does this delete the associated tests as well and > other items that are created when you do script/generate controller > myController? > > Also, it would be nice to have a script/rename controller OrigName NewName > > I know I''ve screwed up more than once naming my stuff the Rails way ;) > > On 5/11/06, *Stephen Bartholomew* <sb@2404.co.uk <mailto:sb@2404.co.uk>> > wrote: > > There''s nothing wrong with just deleting files - although you might not > get all of them. There is a destroy script you can use: > > ruby script/destroy controller ThingController > > or > > ruby script/destroy scaffold Thing > > > Steve > > > c.k.lester wrote: > > I searched Google for this* but didn''t find anything about it from > > casual browsing! Is there an official way to delete > controllers/views, > > or can I just delete folders? > > > > * > http://www.google.com/search?hl=en&q=delete+view+controller+ruby+on+rails&btnG=Google+Search > <http://www.google.com/search?hl=en&q=delete+view+controller+ruby+on+rails&btnG=Google+Search> > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org> > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org> > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Ryan Prins > rprins@gmail.com <mailto:rprins@gmail.com> > http://www.lazyi.net > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > ------------------------------------------------------------------------ > > No virus found in this incoming message. > Checked by AVG Free Edition. > Version: 7.1.392 / Virus Database: 268.5.6/336 - Release Date: 10/05/2006
>From memory you need to put in your application controller.model Foo to tell the controllers that they need to have access to the Foo model. One thing to look out for tho is that if foo changes part way through a session, you will need to update the session object. On 5/12/06, Joe Cairns <joe.cairns@gmail.com> wrote:> > I did some research and am coming up small on the error: > "Session contains objects whose class definition isn''t available. > Remember to require the classes for all objects kept in the session. > (Original exception: uninitialized constant Generic [NameError])" > > I added "require foo.rb" to all of my controllers, but this error > persists. I''ve also tried require_dependencies. > > This error creeps up on the second time a controller is called. The > cause of this problem is when I reference an object in the session like > so: > > @foo= foo.new() > @foo = session[:foo] > @bar = @foo.bar > > Or even when I use the method a previous poster mentioned, like so: > def foo > session[:foo] ? session[:foo] : Foo.new > end > > def foo=(a_foo) > session[:foo] = a_foo > end > > foo.bar > > If I do this: > @foo = Foo.find( session[:site][:id] ) > > ...the problem stops. The trouble is this defeats my purpose of putting > the object into the session in the first place. Any ideas would be > great, I''m at the end of my rope on this one. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060512/7112cb0a/attachment.html
Joe wrote> Here''s what I have: > # Instantiate Foo > @foo = Foo.new() > @foo = session[:foo] > > # Grab Bar with Foo > @bar = @foo.bar > > Is this the bestest Rails way of handling this? Is there a > more concise > way?@foo = Foo.new # this is redundant you don''t need to create an object @foo = session[:foo] # just get the already created one out of the session Not a Ruby developer yet but I suspect you could @bar = session[:foo].bar Which would be about as lean as you could get Ross
Ross Dawson wrote:> Joe wrote >> way? > @foo = Foo.new # this is redundant you don''t need to create an object > @foo = session[:foo] # just get the already created one out of the > session > > Not a Ruby developer yet but I suspect you could > > @bar = session[:foo].bar > > Which would be about as lean as you could get > > RossHi and thanks! However, I was doing that way and it caused this error on invoking the second action: "Session contains objects whose class definition isn''t available. Remember to require the classes for all objects kept in the session. (Original exception: uninitialized constant Generic [NameError])" I have been told to add a model line in every controller where I reference that object in the session, but I have added the code below to all of my controllers with no change of behavior: Model :foo I ended up removing the object from the session and only save foo_id, but then I need to foo.find(session[:foo_id]) on every controller where I need foo. This will cause a lot of unneeded database io. -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-May-12 22:41 UTC
[Rails] Re: RE: Object constructors - Noob Question
On May 12, 2006, at 3:33 PM, Joe Cairns wrote:> Ross Dawson wrote: >> Joe wrote >>> way? >> @foo = Foo.new # this is redundant you don''t need to create an object >> @foo = session[:foo] # just get the already created one out of the >> session >> >> Not a Ruby developer yet but I suspect you could >> >> @bar = session[:foo].bar >> >> Which would be about as lean as you could get >> >> Ross > > Hi and thanks! However, I was doing that way and it caused this error > on invoking the second action: > > "Session contains objects whose class definition isn''t available. > Remember to require the classes for all objects kept in the session. > (Original exception: uninitialized constant Generic [NameError])" > > > I have been told to add a model line in every controller where I > reference that object in the session, but I have added the code > below to > all of my controllers with no change of behavior: > Model :foothat needs to be : model :foo not upcase. -Ezra
Ezra Zygmuntowicz wrote:> On May 12, 2006, at 3:33 PM, Joe Cairns wrote: >>> I have been told to add a model line in every controller where I >> reference that object in the session, but I have added the code >> below to >> all of my controllers with no change of behavior: >> Model :foo > > that needs to be : > > model :foo > > not upcase. > > -EzraHi, it is lower case in code, and looking back on my message, lower case there as well. -- Posted via http://www.ruby-forum.com/.
Joe Cairns wrote:> Ezra Zygmuntowicz wrote: >> On May 12, 2006, at 3:33 PM, Joe Cairns wrote: >> > >>> I have been told to add a model line in every controller where I >>> reference that object in the session, but I have added the code >>> below to >>> all of my controllers with no change of behavior: >>> Model :foo >> >> that needs to be : >> >> model :foo >> >> not upcase. >> >> -Ezra > > Hi, it is lower case in code, and looking back on my message, lower case > there as well.Hi sorry "Model" is upercase in the message only... thought you were talking about :foo. -- Posted via http://www.ruby-forum.com/.
Hi, I''ve narrowed this problem down further, and I probably should have mentioned a few of these details before. "bar" is inherited from "thing" in a single table inheritance structure. I got rid of the error when I took "bar" out of the picture. So I can do the following for example: @foo = session[:foo] puts @foo.id The second I reference "bar" I get zapped with the above message (i.e. bar = @foo.bar). Here is how the models are set up: class Thing < ActiveRecord::Base belongs_to :foo end class Bar < Thing end class Yadda < Thing end class Foo < ActiveRecord::Base has_many :things has_one :yadda has_one :bar validates_uniqueness_of :name end This works the first time that I call @foo.bar (no errors, no warnings), but after the first call all I get is a 500 error and the following in the log file: "Session contains objects whose class definition isn''t available. Remember to require the classes for all objects kept in the session. (Original exception: uninitialized constant Generic [NameError])" In all of my controllers I''ve included: requires ''foo.rb'' requires ''thing.rb'' model :foo model :thing I''m not sure if I''m doing something wrong or if I''m running into some esoteric bug. -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-May-15 03:30 UTC
[Rails] Re: Re: RE: Object constructors - Noob Question
Hi! On May 14, 2006, at 10:31 AM, Joe Cairns wrote:> Hi, I''ve narrowed this problem down further, and I probably should > have > mentioned a few of these details before. > > "bar" is inherited from "thing" in a single table inheritance > structure. > > I got rid of the error when I took "bar" out of the picture. So I can > do the following for example: > @foo = session[:foo] > puts @foo.id > > The second I reference "bar" I get zapped with the above message (i.e. > bar = @foo.bar). Here is how the models are set up: > > class Thing < ActiveRecord::Base > belongs_to :foo > end > class Bar < Thing > end > class Yadda < Thing > end > > class Foo < ActiveRecord::Base > has_many :things > has_one :yadda > has_one :bar > validates_uniqueness_of :name > end > > This works the first time that I call @foo.bar (no errors, no > warnings), > but after the first call all I get is a 500 error and the following in > the log file: > "Session contains objects whose class definition isn''t available. > Remember to require the classes for all objects kept in the session. > (Original exception: uninitialized constant Generic [NameError])" > > In all of my controllers I''ve included: > requires ''foo.rb'' > requires ''thing.rb'' > model :foo > model :thing > > > I''m not sure if I''m doing something wrong or if I''m running into some > esoteric bug. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsI wonder if you are running into rails autorequire feature. One thing to try is to split up your Thing, Bar and Yadda model classes into three separate files called thing.rb, foo.rb and yadda.rb. Try this and see if it helps the problem. You might still need the model :foo declarations as well. -Ezra
Joe Cairns
2006-May-15 06:01 UTC
[Rails] Re: Re: Re: RE: Object constructors - Noob Question
Ezra Zygmuntowicz wrote:> Hi! > > On May 14, 2006, at 10:31 AM, Joe Cairns wrote: >> > > I wonder if you are running into rails autorequire feature. One > thing to try is to split up your Thing, Bar and Yadda model classes > into three separate files called thing.rb, foo.rb and yadda.rb. Try > this and see if it helps the problem. You might still need the > model :foo declarations as well. > > > -EzraHi and thanks, I tried this right away as it seemed to be a good solution, but the exact same behavior is happening. It works the first time, and the second I get the 500 error. The only way to get the app running again is to delete the session or clear the browser cache. The very weird thing about this is the message in the log file mentioning the session. The error only occurs when I introduce bar into the equation, running @bar = @foo.bar and bar never touches the session at all. @foo is instantiated from the session, by @foo = session[:foo] but this _doesn''t_ cause the error. In fact I can use @foo in views and other ways and no error occurs. I really have no idea what''s going on with this. -- Posted via http://www.ruby-forum.com/.
Ross Dawson
2006-May-15 06:09 UTC
[Rails] Re: Re: Re: RE: Object constructors - Noob Question
> @foo is instantiated from the session, by @foo = > session[:foo] but this > _doesn''t_ cause the error. In fact I can use @foo in views and other > ways and no error occurs. > > I really have no idea what''s going on with this.Try using breakpoint to inspect @foo http://wiki.rubyonrails.com/rails/pages/HowtoDebugWithBreakpoint can tell you lots. As a general "why doesn''t my blah work" response have a look at your code running using breakpoint. I''m guessing from the noise on the list that breakpoint would be the most underutilized tool in Rails. Ross