zoukankan      html  css  js  c++  java
  • Rails NameError uninitialized constant class solution

    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_date
    	Time.now.strftime(‘%F’)
       end
    end

    And then if you want to call the function through command line i.e. rails console probably you will start rails console and 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
      end
    end

    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 reduced if you configure your Rails application properly. ReadConfiguring Rails application to know more about configurations.

  • 相关阅读:
    是否该让开发人员跟客户直接交流 狼人:
    2010年浏览器随HTML5而动 五大产品年终盘点 狼人:
    微软推出HTML5实验室站点及两项原型技术 狼人:
    传IE9 RC版将于1月28日公开发布 狼人:
    Python——基础篇 狼人:
    百万级访问量网站的技术准备工作 狼人:
    容器对象spring(4)_ bean属性 scope:作用域和lazyinit
    组件注册关于VC++6.0中,MSDev89\Gallery 文件夹为空的问题
    注入参数spring入门(7)装配Bean中构造参数的注入
    优惠播客成都传智播客java基础班大优惠
  • 原文地址:https://www.cnblogs.com/juandx/p/3893358.html
Copyright © 2011-2022 走看看