Object constructors - Noob Question

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?

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

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?

*delete view controller ruby on rails - Google Search

./script/destroy controller_name

On 5/11/06, c.k.lester [email protected] wrote:


Ben R.
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

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.

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 :wink:

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

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.

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
:wink:
Not using the ‘Rails’ way? I sense the darkside in you… :0)

Steve