0

I want to define Global Constant in Rails 3 but I am confuse looking so many differnet answer on Google.

I was trying this solution but it did not work and gave error:

Undefined method `music_type' for #<Rails::Application::Configuration:0xb7ac0230>

In /config/application.rb

module RailsRoot
 Class Application < Rails :: Application
 config.music_type = '2'
 end
end

In Controller

RailsRoot::Application::config.music_type

not sure what I am doing wrong.

Thanks

Community
  • 1
  • 1
Viral
  • 310
  • 3
  • 19

2 Answers2

1

I think, the best way is to define you own initializer in config/initializers/ folder.

Example:

Create my_initializer.rb in config/initializers/ folder with following content:

require 'socket'

def local_ip
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off      reverse DNS resolution temporarily

  UDPSocket.open do |s|
    s.connect '64.233.187.99', 1
    s.addr.last
  end
ensure
  Socket.do_not_reverse_lookup = orig
end

SERVER_IP = local_ip

and in some controller use that constant:

log_it "Server IP address is: #{SERVER_IP}"
Jiemurat
  • 1,619
  • 20
  • 20
1

you need a dedicated gem for that, smth like http://rubygems.org/gems/rails-3-settings

this thread has a detailed answer

Community
  • 1
  • 1
zed_0xff
  • 32,417
  • 7
  • 53
  • 72