Forum: Ruby on Rails Where do i put classes that i want to use in Controller??

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.
James S. (Guest)
on 2007-01-28 15:36
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?
Elliott B. (Guest)
on 2007-01-28 16:29
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
James S. (Guest)
on 2007-01-28 18:59
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??
unknown (Guest)
on 2007-01-28 19:08
(Received via mailing list)
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 (http://www.manning.com/black)
    (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)
Stephen B. (Guest)
on 2007-01-28 19:09
(Received via mailing list)
> 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
James S. (Guest)
on 2007-01-28 19:27
> 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
Stephen B. (Guest)
on 2007-01-28 23:33
(Received via mailing list)
>  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
This topic is locked and can not be replied to.