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.
612a5333e356c4f765230d9ac06e5c8c?d=identicon&s=25 Pradeep Sethi (psethi2000)
on 2006-05-01 21: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
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2006-05-01 22: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 Sethi 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
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2006-05-01 23: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'
>
612a5333e356c4f765230d9ac06e5c8c?d=identicon&s=25 Pradeep Sethi (psethi2000)
on 2006-05-02 06: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'
>>
6661ef9d747db3af8896cd94959d717d?d=identicon&s=25 Paul Barry (Guest)
on 2006-05-03 18: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.
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2006-05-03 18:59
(Received via mailing list)
2006/5/2, Pradeep Sethi <psethi@gmail.com>:
> 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.