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.

  1. Class Util
  2. def self.get_date
  3. Time.now.strftime(‘%F’)
  4. end
  5. 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,

  1. rails console
  2. > 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

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

Your application.rb will look something like,

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

  1. 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.

Rails NameError uninitialized constant class solution的更多相关文章

  1. 解决脱离rails使用activerecord报错 NameError: uninitialized constant ActiveRecord::Migrator::Zlib

    上下文说明 原本系统是15.10,无奈只支持1年,所以今天升级16.04,环境答好后运行rake migratte报错 task :default => :migrate desc 'Run m ...

  2. 有意练习--Rails RESTful(一)

    书要反复提及<哪里有天才>在说,大多数所谓的天才是通过反复刻意练习获得. 当你的练习时间达到10000几个小时后,.你将成为该领域的专家. 近期在学习rails怎样实现RESTful We ...

  3. 生产环境rails console spring自动启动的问题

    在生产环境执行rails console没反应无法进入控制台,或者执行rails console的时候spring自动启动,导致所有的类名都无法识别,报错:NameError: uninitializ ...

  4. ruby学习之路(一)

    学习ruby最好的方法就是下载源码包,里面带有sample和test,是入门学习的最好实例. 我下载的是2.1.0版本,首先./configure,然后make,sudo make install.从 ...

  5. ruby-on-rails-BUG

    Ruby on Rails errors: (新手期) rails 自动化部署教程 Q1: rvm 无法使用 $ rvm use 1.9.3 --default RVM is not a functi ...

  6. kali Linux系列教程之BeFF安装与集成Metasploit

    kali Linux系列教程之BeFF安装与集成Metasploit 文/玄魂 kali Linux系列教程之BeFF安装与集成Metasploit 1.1 apt-get安装方式 1.2 启动 1. ...

  7. Centos 6 搭建安装 Gitlab

    官方安装教程 gitlab / gitlab-ce 官网下载:https://www.gitlab.cc/downloads 官网安装说明:https://doc.gitlab.cc/ce/insta ...

  8. redis集群离线安装环境搭建过程

    本文是继上次redis集群重新整理的离线搭建环境,关于前期的redis集群准备工作参考我另一篇博客: http://www.cnblogs.com/qlqwjy/p/8566573.html 由于集群 ...

  9. 编译的 Ruby 2.3.0 缺少 openssl 支持的解决方法 (已解决)

    我的系统是centos 7.5,已离线安装ruby-2.3.0,openssl-1.0.2l,rubygems-2.7.4 如下图: 但是在  gem sources -a http://gems.r ...

随机推荐

  1. web.csproj Compile 下出现两个同名 xxx.cs 项目中出现两个xxx.cs

    删掉一个就好了 ItemGroup Compile 为加载的cs代码文件

  2. python练习笔记——用列表推导式生成二维列表

    用列表推导式如何生成如下列表:[[1, 2, 3], [4, 5, 6], [7, 8, 9]] inner_list = [] outer_list = [] for i in range(1,10 ...

  3. Linux时间子系统(五) POSIX Clock

    一.前言 clock是timer的基础,任何一个timer都需要运作在一个指定的clock上来.内核中维护了若干的clock,本文第二章描述了clock的基本概念和一些静态定义的posix clock ...

  4. mysql-5.7 innodb change buffer 详解

    一.innodb change buffer 介绍: 1.innodb change buffer 是针对oltp场景下磁盘IO的一种优化(我也感觉这个不太像人话,但是它又非常的准确的说明 innod ...

  5. CentOS安装Webmin

    解析:Webmin是目前功能最强大的基于Web的Unix系统管理工具.管理员通过浏览器访问Webmin的各种管理功能并完成相应的管理动作.目前 Webmin支持绝大多数的Unix系统,这些系统除了各种 ...

  6. 【Android】3.17 示例17--周边雷达功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 周边雷达功能同步支持Android和iOS端.它本质是一个连接百度LBS开放平台前端SDK产品和后端 ...

  7. 解决方式-在Mac系统中,Eclipse无法导入含有中文路径的project

    1.改动eclipse.app/Contents/Info.plist.查找 <key>CFBundleExecutable<key> 在其上方加入下面代码 <? xml ...

  8. UnityTestTools測试工具

    由于工作关系,要了解Unity上的測试工具,该工具基于Nunit框架.通过查阅资料了解到在Unity5.3中做出了一些改变,自带的仅仅剩下单元測试工具,假设想用其它的工具比方断言.集成測试,就须要前往 ...

  9. windows 7 IIS 7.0 装好后,HTTP Error 503. The service is unavailable.

    IIS 7.0 装好后,出现 以上问题. 解决办法,如下图,设置应用程序池 ,

  10. GPIO实验(一)

    目标:点亮LED1.看原理图,找到对应的引脚和寄存器2.a.配置寄存器为输入/出引脚    GPFCON[9:8]=0b01  b.设置输出高/低电平    GPDAT[4]=0b0 1.预处理2.编 ...