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.

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. Python学习笔记015——序列(字节数组 bytearray)

    1 序列 常见的序列有:list   tuple  str  bytes(字节串)  bytearray 2 字节数组bytearray 可变的字节序列,相当于bytes的可变版本. 3 创建函数by ...

  2. oracle导表小结

    事件描述:从A主机oracle服务器导出.sql文件到B主机,发现1.导入存在乱码 2.提示USERS表空没有权限(A B主机均为window系统) 1.针对第一点乱码 首先确认系统的默认字符编码GB ...

  3. Unix环境高级编程(十四)守护进程实现时间服务器

    守护进程是在后台运行不受终端控制的进程(如输入.输出等),一般的网络服务都是以守护进程的方式运行.守护进程脱离终端的主要原因有两点:(1)用来启动守护进程的终端在启动守护进程之后,需要执行其他任务.( ...

  4. Spark与Pandas中DataFrame对比

      Pandas Spark 工作方式 单机single machine tool,没有并行机制parallelism不支持Hadoop,处理大量数据有瓶颈 分布式并行计算框架,内建并行机制paral ...

  5. spring 中 AOP 功能

    1 PointCut 由 ClassFilter 和 MethodMatcher 构成,通过 ClassFilter 定位到类上,通过 MethodMatcher 定位到方法. 2 Spring 支持 ...

  6. 百度地图地址解析(百度Geocoding API)

    1.什么是Geocoding? Geocoding API 是一类简单的HTTP接口,用于提供从地址到经纬度坐标或者从经纬度坐标到地址的转换服务,用户可以使用C# .C++.Java等开发语言发送HT ...

  7. java Webservice(一)HttpClient远程调用

    我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种: 1.通过浏览器HTTP调用,返回规范的XML文件内容      2.通 ...

  8. [Jobdu] 题目1500:出操队形

    题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较高的就要排在队尾.突然,有一天出操负责人想了一个 ...

  9. iOS学习笔记37-时间和日期计算

    一.时间和日期计算 我们在应用开发中,时常须要和时间打交道,比方获取当前时间,获取两个时间点相隔的时间等等,在iOS开发中与时间相关的类有例如以下几个: 1. NSDate:表示一个绝对的时间点 2. ...

  10. android的一些控件

    原来朋友给过的一个 显示时间的 样例,还能够改动时间,可是要机子有root权限才干改动. 在这个时间表盘的样例基础上 改动改动  图片.背景图什么的      就能够达到自己想要的效果了.. 下载地址 ...