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
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/HowtoAddYourOwnConfigInfo Craig White wrote:> 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- -- I do things for love or money
On Wed, 2006-03-15 at 01:20 -0500, Dorian Mcfarland wrote:> 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/HowtoAddYourOwnConfigInfo > > Craig White wrote: > > 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 > >---- 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
On Wed, 2006-03-15 at 01:20 -0500, Dorian Mcfarland wrote:> 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/HowtoAddYourOwnConfigInfo > > Craig White wrote: > > 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?---- 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
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 Craig White wrote:> On Wed, 2006-03-15 at 01:20 -0500, Dorian Mcfarland wrote: >> 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/HowtoAddYourOwnConfigInfo >> >> Craig White wrote: >>> 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? > ---- > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- -- I do things for love or money
> 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"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 - dfaltIn 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. > > Craigregards Claus
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 On Wed, 2006-03-15 at 01:56 -0500, Dorian Mcfarland wrote:> 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 > > Craig White wrote: > > On Wed, 2006-03-15 at 01:20 -0500, Dorian Mcfarland wrote: > >> 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/HowtoAddYourOwnConfigInfo > >> > >> Craig White wrote: > >>> 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? > > ---- > > 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 > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 On Wed, 2006-03-15 at 08:01 +0100, Claus Guttesen wrote:> > 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" > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> 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. > > thanksThanks to you as well :-) regards Claus
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
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 - dfaltYou 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
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 wikiWhile 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
On Mar 15, 2006, at 7:12 AM, dblack@wobblini.net wrote:> 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.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
On Wed, 2006-03-15 at 05:12 -0800, dblack@wobblini.net wrote:> 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. >---- done - thanks Craig