Where do i put classes that i want to use in Controller?

Hello,

Can someone please help me…

I would like to know where i should put classes that i need to use
within my controller?

And how are they accessible to the controller?

James S. wrote:

Hello,

Can someone please help me…

I would like to know where i should put classes that i need to use
within my controller?

And how are they accessible to the controller?

If it is a controller class, I’d put it in your app/controllers
directory. Model classes in app/models, etc.

app/contollers/your_controller.rb

def YourController < ActiveConroller
ModelClass.method()
end

app/models/model_class.rb

def ModelClass < ActiveRecord::Base
def method
end
end

Elliott B. wrote:

If it is a controller class, I’d put it in your app/controllers
directory. Model classes in app/models, etc.

It is neither… Its just an additional class that i need to create
objects for use within my controller, any ideas??

Its just an additional class that i need to create
objects for use within my controller, any ideas??
If you create a class and put it in lib/, you’ll be able to access it
from anywhere in your application - including controllers.

Just name the file after the class name i.e. MyClass should be in lib/
my_class.rb. If you’re using 1.2, rails will include the file
automatically. If you’re still on an earlier version, you’ll need to
add a require line in yourself in environment.rb (require ‘my_class’).

Hope that helps,

Steve

Hi –

On Sun, 28 Jan 2007, James S. wrote:

Elliott B. wrote:

If it is a controller class, I’d put it in your app/controllers
directory. Model classes in app/models, etc.

It is neither… Its just an additional class that i need to create
objects for use within my controller, any ideas??

It sounds like a candidate for a file in lib/ .

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Hope that helps,

Yes, thanks very much.

One last question, if thats OK? When my application starts i need to do
some basic setup work. This just involves calling a method that creates
some objects, that are stored in the @session, based on data retrieved
from the database. Can you advise me as to where this method should be
called from (just the index of the first controller??) and where this
‘setup’ method should be within my rails directory structure?

Thanks again

where this
‘setup’ method should be within my rails directory structure?
What I would do is add a before_filter to your ApplicationController:

class ApplicationController < ActionController:Base
before_filter :setup

private
def setup
if !session[:my_variable]
# get data and edit session
end
end
end

Just to note, you should use the ‘session’ method to access the
session data rather than the instance variable ‘@session’. You should
also consider whether or not you really need to store objects in the
session as it can cause issues. It’s best to keep the session small
if you can.

Hope that helps,

Steve