Forum: Ruby on Rails usage of constants

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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 07:06
(Received via mailing list)
I would like to define some constants that I can use across models and
views and I cannot figure out how to do that. Googling hasn't turned up
anything useful and I'm looking at the PickAxe book, and I get the
impression that a constant must belong to a class.

If I just want a constant like...

  CompanyPhoneNo     = "(602) 999-9999"

and be able to use it in any view with any controller, how do I do that?

Craig
F719c6cb34bd5b691c12b373b7244b4d?d=identicon&s=25 Dorian Mcfarland (tachekent)
on 2006-03-15 07:21
(Received via mailing list)
I think you can put constants/configs in environments.rb at the bottom.
There's a good explanation here:
http://wiki.rubyonrails.com/rails/pages/HowtoAddYo...
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 07:27
(Received via mailing list)
On Wed, 2006-03-15 at 01:20 -0500, Dorian Mcfarland wrote:
> >
> >   CompanyPhoneNo     = "(602) 999-9999"
> >
> > and be able to use it in any view with any controller, how do I do that?
> >
> > Craig
> >
----
yeah - that's what I was looking for. The funny thing was..."All Pages"
wiki.rubyonrails.org was where I started but I searched for 'Constants'
- duh

Craig
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 07:48
(Received via mailing list)
On Wed, 2006-03-15 at 01:20 -0500, Dorian Mcfarland wrote:
> >
> >   CompanyPhoneNo     = "(602) 999-9999"
> >
> > and be able to use it in any view with any controller, how do I do that?
----
you know...this is one of the things that sort of sucks about
rails...that which should be ridiculously simple is poorly documented
and difficult to accomplish...

that pages says...

Just another ruby file
Place your configuration constants in a regular Ruby file. include the
file where you need the settings. For example, this is my
config/email.conf.rb


#
# Email configuration
#
$SMTP_ADDRESS        = "smtp.mac.com"
$SMTP_PORT        = 25
$SMTP_DOMAIN        = "mail.mac.com"
$SMTP_USER_NAME        = "johnatl"
$SMTP_USER_PASSWORD    = "secret"

so I made a file... config/defaults.rb

In the controller section, I try...

    :include ../config/defaults.rb
SyntaxError in <controller not set>#<action not set>
./script/../config/../app/controllers/reports_controller.rb:21: unknown
regexp options - dfalt

    :include "../config/defaults.rb"
SyntaxError in <controller not set>#<action not set>
./script/../config/../app/controllers/reports_controller.rb:21: syntax
error, unexpected tSTRING_BEG, expecting kEND
    :include "../config/defaults.rb"

    :include => "../config/defaults.rb"
SyntaxError in <controller not set>#<action not set>
./script/../config/../app/controllers/reports_controller.rb:21: syntax
error, unexpected tASSOC, expecting kEND
    :include => "../config/defaults.rb"

    include "../config/defaults.rb"
NoMethodError in Reports#rfn2
undefined method `include' for #<ReportsController:0xb7d003d4>

so that means in order to get a few stupid default values, I have to
create a table and keep hitting the table all the time.

Craig
F719c6cb34bd5b691c12b373b7244b4d?d=identicon&s=25 Dorian Mcfarland (tachekent)
on 2006-03-15 07:57
(Received via mailing list)
where are you setting that in the controller file?

I think you should place:

include "../config/defaults.rb"

... *before* the class declaration.

Also, if you place it in the apllication.rb controller, it will be
accessible everywhere.

dorian
B8d11b4c89cfc6e34d6c9fb121c9acac?d=identicon&s=25 Claus Guttesen (Guest)
on 2006-03-15 08:04
(Received via mailing list)
> $SMTP_PORT        = 25
> $SMTP_DOMAIN        = "mail.mac.com"
> $SMTP_USER_NAME        = "johnatl"
> $SMTP_USER_PASSWORD    = "secret"

Looks like perl-code to me. Try changing the $-sign with a @ instead.

> so I made a file... config/defaults.rb
>
> In the controller section, I try...
>
>     :include ../config/defaults.rb
> SyntaxError in <controller not set>#<action not set>
> ./script/../config/../app/controllers/reports_controller.rb:21: unknown regexp options - 
dfalt

In ruby the include statement is called require or load. Try changing
the include statement.

> so that means in order to get a few stupid default values, I have to create a table and 
keep hitting the table all the time.
>
> Craig

regards
Claus
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 08:04
(Received via mailing list)
this was before the class declaration in application.rb
TypeError in <controller not set>#<action not set>
wrong argument type String (expected Module)

RAILS_ROOT: script/../config/..

Application Trace | Framework Trace | Full Trace
./script/../config/../app/controllers/application.rb:3:in `include'
./script/../config/../app/controllers/application.rb:3
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:207:in
`load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:39:in
`require_or_load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:22:in
`depend_on'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:178:in
`require_dependency'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:134:in
`load_file!'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:97:in
`const_load!'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:80:in
`const_load!'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:71:in
`prepare_application'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:37:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
`handle_dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in
`service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
`require'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
`require'
script/server:3
./script/../config/../app/controllers/application.rb:3:in `include'
./script/../config/../app/controllers/application.rb:3
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:207:in
`load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:39:in
`require_or_load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:22:in
`depend_on'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:178:in
`require_dependency'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:134:in
`load_file!'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:97:in
`const_load!'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:80:in
`const_load!'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:71:in
`prepare_application'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:37:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
`handle_dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in
`service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
`require'
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
`require'
script/server:3
This error occured while loading the following files:
   script/../config/../app/controllers/application.rb
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 08:10
(Received via mailing list)
I was under the impression that @ was an instance variable that wouldn't
persist but $ was a constant.

Turns out that 'include' wasn't correct but 'require' was the juju
necessary and all I had to do was fix the path.

I will have to change the wiki

thanks

Craig
B8d11b4c89cfc6e34d6c9fb121c9acac?d=identicon&s=25 Claus Guttesen (Guest)
on 2006-03-15 08:13
(Received via mailing list)
> I was under the impression that @ was an instance variable that wouldn't
> persist but $ was a constant.

You're correct.

> Turns out that 'include' wasn't correct but 'require' was the juju
> necessary and all I had to do was fix the path.
>
> thanks

Thanks to you as well :-)

regards
Claus
2899cec9af525f7953e19c8210aca97c?d=identicon&s=25 Jason Perkins (Guest)
on 2006-03-15 13:55
(Received via mailing list)
On Mar 15, 2006, at 1:07 AM, Craig White wrote:

> I was under the impression that @ was an instance variable that
> wouldn't
> persist but $ was a constant.

$ precedes global variables. Constants have their initial character
upper cased.

--
Jason Perkins
jperkins@sneer.org

"The key to performance is elegance, not
  battalions of special cases."
  - Jon Bentley and Doug McIlroy
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-15 14:11
(Received via mailing list)
Hi --

On Tue, 14 Mar 2006, Craig White wrote:

> In the controller section, I try...
>
>    :include ../config/defaults.rb
> SyntaxError in <controller not set>#<action not set>
> ./script/../config/../app/controllers/reports_controller.rb:21: unknown regexp options - 
dfalt

You have to put your filename in quotation marks.  Otherwise Ruby sees
forward slashes and thinks you're trying to write a regular expression
literal.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-15 14:15
(Received via mailing list)
Hi --

On Wed, 15 Mar 2006, Craig White wrote:

> I was under the impression that @ was an instance variable that wouldn't
> persist but $ was a constant.
>
> Turns out that 'include' wasn't correct but 'require' was the juju
> necessary and all I had to do was fix the path.
>
> I will have to change the wiki

While you're there, please also change "configuration constants" to
"configuration global variables", to suit the sample code.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
2899cec9af525f7953e19c8210aca97c?d=identicon&s=25 Jason Perkins (Guest)
on 2006-03-15 14:34
(Received via mailing list)
On Mar 15, 2006, at 7:12 AM, dblack@wobblini.net wrote:

>>
>> I will have to change the wiki
>
> While you're there, please also change "configuration constants" to
> "configuration global variables", to suit the sample code.


In the provided code sample would it be more Rubyist to make those
globals into constants? Actually there are two cases there that
clarification would be good on with this scoping issue: the
environment.rb and the included config.rb files. Better to use
constants in each of those or one and not the other?

BTW, very much enjoying your book. New chapter(s) for us today? ;)


--
Jason Perkins
jperkins@sneer.org

"The key to performance is elegance, not
  battalions of special cases."
  - Jon Bentley and Doug McIlroy
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 15:23
(Received via mailing list)
On Wed, 2006-03-15 at 05:12 -0800, dblack@wobblini.net wrote:
> > I will have to change the wiki
>
> While you're there, please also change "configuration constants" to
> "configuration global variables", to suit the sample code.
>
----
done - thanks

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