Forum: Ruby on Rails Object constructors - Noob Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-05-11 16:49
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?
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-11 17:00
(Received via mailing list)
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
D48aba5b6c3d0d7b6e80571e895a668c?d=identicon&s=25 c.k.lester (Guest)
on 2006-05-11 17:10
(Received via mailing list)
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+c...
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-05-11 17:13
(Received via mailing list)
./script/destroy controller_name

On 5/11/06, c.k.lester <rails@cklester.com> wrote:
>
--
Ben Reubenstein
303-947-0446
http://www.benr75.com
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-11 17:17
(Received via mailing list)
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
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-05-11 17:45
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.bar

Ok 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
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-05-11 20:20
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.
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-05-11 22:10
(Received via mailing list)
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 ;)
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-11 23:40
(Received via mailing list)
> 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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-12 03:35
(Received via mailing list)
>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.
This topic is locked and can not be replied to.