If I do my own class, of something, and want to access it in my app /
controllers, where do I put it? Is /lib the place?
How do I access it?
It’s about Rails but at the same time more of a Ruby question, so I hope
that’s ok.
If I do my own class, of something, and want to access it in my app /
controllers, where do I put it? Is /lib the place?
How do I access it?
It’s about Rails but at the same time more of a Ruby question, so I hope
that’s ok.
Ryan B. wrote:
Correct, lib is the place.
Thanks. And how do I access it in my controller?
Correct, lib is the place.
Just like any normal class (usually)… you may have to require the
file in config/environment.rb, but I think files in lib are
automatically loaded.
Depending on what type of class you’re thinking of: you can also
create a class in the app/models folder, even if your model won’t be
an ActiveRecord model (so no associated database table). Think of
using that if your class is really a model, so creating objects that
you want to view and manipulate in the usual MVC way. For example a
class of static objects that can be auto-generated at runtime (and
don’t need a database table because they will never change) like the
12 months of a year could be encapsulated in a model.
Dirk.
On 20 aug, 08:09, Pål Bergström [email protected]
Ryan B. wrote:
Just like any normal class (usually)… you may have to require the
file in config/environment.rb, but I think files in lib are
automatically loaded.
Perfect.
deegee wrote:
Depending on what type of class you’re thinking of: you can also
create a class in the app/models folder, even if your model won’t be
an ActiveRecord model (so no associated database table). Think of
using that if your class is really a model, so creating objects that
you want to view and manipulate in the usual MVC way. For example a
class of static objects that can be auto-generated at runtime (and
don’t need a database table because they will never change) like the
12 months of a year could be encapsulated in a model.Dirk.
On 20 aug, 08:09, P�l Bergstr�m [email protected]
I see. That makes sense. Just a thought, but perhaps that would be the
place to put different language variables? If a user set his/here
language settings to ‘swe’ all variables that is in there and in the app
would output the right word in Swedish instead of English – that I’ve
statically entered in a class.
yeah you r right .
you may have to require the file in config/environment.rb,
Nope, no need
but I think files in lib are automatically loaded.
Yes they are.
Hi,
Different language variables bundled in a class I would definetely put
in the lib folder like Ryan suggested; also if you want to override or
extend some standard Rails classes, that’s where you should do it. The
other options you have apply to more specific cases:
Cheers,
Dirk.
On 20 aug, 10:30, Pål Bergström [email protected]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs