简体   繁体   中英

Rails 4 constants - 'Unitialized constant' error

I'm attempting to use global constants in a Rails 4 app to validate certain elements, such as emails, ethnic groups, etc. I've followed the approach outlined at the stack overflow article "How do you store custom constants in Rails 4" , without success.

I've defined a file lib/constants.rb as the location for all application-wide constants. In my config/application.rb file I've included the following code:

module Foo
  class Application < Rails::Application
    # extraneous code omitted 

    config.autoload_paths << Rails.root.join('lib') # added this line to include lib directory
  end
end

I've tried a couple of options for defining and loading the constants, but I'm still getting errors.

Attempt 1: created a file lib/constants.rb with code defining a constant:

ETHNIC_GROUPS = ["N/A", "African American/Black", "American Indian", "Hispanic", "Puerto Rican",
      "Alaskan Native", "Asian American/Pacific Islander", "Mexican American/Chicano"]

Attempt 2: Based on discussion at article "Auto-loading lib files in Rails 4" I modified the filename and code a bit to try and load the constants another way. Per that article I've defined a file lib/Foo.rb (matches the application name in config/application.rb ):

module Foo
  # Define custom constants for the application
  ETHNIC_GROUPS = ["N/A", "African American/Black", "American Indian", "Hispanic", "Puerto Rican",
      "Alaskan Native", "Asian American/Pacific Islander", "Mexican American/Chicano"]
end

This looks like it would essentially monkey patch, or add to the existing Foo module defined in config/application.rb - my assumption, anyway.

I've restarted the server, reloaded the console, etc after each change. No matter how many times I do so, I continue to get the uninitialized constant error. In the console, I've attempted to call both ETHNIC_GROUPS and Foo::ETHNIC_GROUPS without success.

Appreciate any insight on best practices for working with global application constants in Rails 4, as well as insight into why this isn't working for me.

I recommend you " rails_config " gem: Github

It allows you to put any number of settings in an .yml file. It also allows to create different settings files for each environment.

A common method would be to create a common_constant.rb file in config/initializers and add your constants there like

module CommonConstants
  MY_VARIABLE = 'foo'
  MY_HASH = {
    foo: 'more foo',
    bar: 'big bar'
  }
  MY_ARRAY = ['foo','bar']
end

and to access it

CommonConstants::MY_VARIABLE

Appreciate the answers! I realized that the issue was defining my module in lib/Foo.rb to try and match the application module defined in config/application.rb . I would expect that to work, but what did work is:

  1. Defined a AppConstants module in lib/app_constants.rb :

     module AppConstants ETHNIC_GROUPS = ["N/A", "African American/Black", "American Indian", "Hispanic", "Puerto Rican", "Alaskan Native", "Asian American/Pacific Islander", "Mexican American/Chicano"] end 
  2. Called the constant with AppConstants::ETHNIC_GROUPS in the code.

With the (previously defined) code in application.rb to autoload the lib directory, this got pulled into the application correctly.

The same result could have been achieved with either the 'rails_config' gem, or with using a config/initializers/common_constants.rb file as suggested by @Ojash.

In the end I decided I prefer having the constants in a distinct file within the lib directory. This seems a bit cleaner to me. I decided I'd rather not pull in another gem with code I don't necessarily understand.

I don't know why the constant wasn't getting referenced when calling Foo::ETHNIC_GROUPS as noted in Option 2 in my original post. The discussion in the article about Auto-loading lib files in Rails 4 seemed to recommend using a lib module with the same name as the application module. After I tried renaming the module it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM