Forum: Ruby on Rails Adding classes without script\generate

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.
Pradeep S. (Guest)
on 2006-05-01 23:48
Can I add classes to a rails application manually i.e without using
script generate Model/Controller?

The reason I am asking, I added a class by putting it in the controller
class. I can access the class and its method from command-line (irb),
but when I try to instantiate it in another controller, I get
"uninitialized constant" error.

If I try using script\generate Model ClassName, it adds "<
ActiveRecord::Base", where as I need to inherit another class.

ClassName <  ActiveRecord::Base

Thanks,


Pradeep
Ilan B. (Guest)
on 2006-05-02 00:51
Pradeep,

1) If this class is meant to be a model then it should inherit from
ActiveRecord::Base.

2) I hand write classes all the time, there is no need to use the model
generator if you don't want to.

3) You most probably need a require statement in your controller file.
i.e.  require 'class_name'

4) If it's a model, you will have to put a "model
:model_name_in_singular_lower_case" in your controller that accesses it

5) You are getting the constant not recognized error since the
interpreter can't resolve your class.

I hope this helps and please be advised that I am not an expert in this
area.

ilan



Pradeep S. wrote:
> Can I add classes to a rails application manually i.e without using
> script generate Model/Controller?
>
> The reason I am asking, I added a class by putting it in the controller
> class. I can access the class and its method from command-line (irb),
> but when I try to instantiate it in another controller, I get
> "uninitialized constant" error.
>
> If I try using script\generate Model ClassName, it adds "<
> ActiveRecord::Base", where as I need to inherit another class.
>
> ClassName <  ActiveRecord::Base
>
> Thanks,
>
>
> Pradeep
Ilan B. (Guest)
on 2006-05-02 01:02
whoops.. I meant require 'file name'.. damn my fingers.. :)

> 3) You most probably need a require statement in your controller file.
> i.e.  require 'class_name'
>
Pradeep S. (Guest)
on 2006-05-02 08:43
I figured out the error.

I needed to restart my server every time, I make a change to the class
file (because it is not a controller and/or Model...)

feel like stupi..... :)

ilan berci wrote:
> whoops.. I meant require 'file name'.. damn my fingers.. :)
>
>> 3) You most probably need a require statement in your controller file.
>> i.e.  require 'class_name'
>>
Paul B. (Guest)
on 2006-05-03 20:58
(Received via mailing list)
Are you using just regular irb or script/console?  You need to use
script/console or else your class won't be available.
Douglas L. (Guest)
on 2006-05-03 20:59
(Received via mailing list)
2006/5/2, Pradeep S. <removed_email_address@domain.invalid>:
> I figured out the error.
>
> I needed to restart my server every time, I make a change to the class
> file (because it is not a controller and/or Model...)
>
> feel like stupi..... :)
>

In 1.1, you can add "include Reloadable" in the model so that you
don't have to restart the whole server.

Douglas
This topic is locked and can not be replied to.