Rails nameerror uninitialized constant class Solution

Source: Internet
Author: User
Tags i18n time zone names
Rails nameerror uninitialized constant class will occur if your rails console is not loaded with configuration of the class file containing method being called. Problem

Basically this can happen if you happened to call the function from the class which is not loaded with configuration when your rails console/server was started.

Example

Suppose you create a file named util. Rb in the Lib folder of your rails project.

Class Util   def self.get_dateTime.now.strftime(‘%F’)   endend

And then if you want to call the function through command line I. e. rails console probably you will start rails console and call the method get_date using class name util as follows,

rails console> Util.get_date

This will give you following error-
Nameerror: uninitialized constant util
It means that your class was not loaded when the rails console was started

Solution

To resolve this problem your class has to be loaded in environment, this can be done in following way,

1. open application. RB file of your rails Project
2. Add following line in application. Rb

config.autoload_paths += %W(#{config.root}/lib)

Your application. RB will look something like,

require File.expand_path(‘../boot‘, __FILE__)require ‘rails/all‘# Require the gems listed in Gemfile, including any gems# you‘ve limited to :test, :development, or :production.Bundler.require(:default, Rails.env)module RubyInRailsApp  class Application < Rails::Application    # the new line added for autoload of lib    config.autoload_paths += %W(#{config.root}/lib)    # Settings in config/environments/* take precedence over those specified here.    # Application configuration should go into files in config/initializers    # -- all .rb files in that directory are automatically loaded.    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.    # config.time_zone = ‘Central Time (US & Canada)‘    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.    # config.i18n.load_path += Dir[Rails.root.join(‘my‘, ‘locales‘, ‘*.{rb,yml}‘).to_s]    # config.i18n.default_locale = :de  endend

3. Done

Conclusion

Thus, setting autoload_paths for config in application. RB will result for-Ruby files present in lib directory of your rails project to get automatically loaded when your console is started. calling method will not give rails nameerror uninitialized constant class error anymore.
Now, calling the method-

Util.get_date

Will return result as expected. As the class was loaded with configuration when your console was started thus name resolution was successful.

Ultimately these kind of errors can be implicitly CED if you configure your rails application properly. readconfiguring rails application to know more about deployments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.