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.
Rails NameError uninitialized constant class solution的更多相关文章
- 解决脱离rails使用activerecord报错 NameError: uninitialized constant ActiveRecord::Migrator::Zlib
上下文说明 原本系统是15.10,无奈只支持1年,所以今天升级16.04,环境答好后运行rake migratte报错 task :default => :migrate desc 'Run m ...
- 有意练习--Rails RESTful(一)
书要反复提及<哪里有天才>在说,大多数所谓的天才是通过反复刻意练习获得. 当你的练习时间达到10000几个小时后,.你将成为该领域的专家. 近期在学习rails怎样实现RESTful We ...
- 生产环境rails console spring自动启动的问题
在生产环境执行rails console没反应无法进入控制台,或者执行rails console的时候spring自动启动,导致所有的类名都无法识别,报错:NameError: uninitializ ...
- ruby学习之路(一)
学习ruby最好的方法就是下载源码包,里面带有sample和test,是入门学习的最好实例. 我下载的是2.1.0版本,首先./configure,然后make,sudo make install.从 ...
- ruby-on-rails-BUG
Ruby on Rails errors: (新手期) rails 自动化部署教程 Q1: rvm 无法使用 $ rvm use 1.9.3 --default RVM is not a functi ...
- kali Linux系列教程之BeFF安装与集成Metasploit
kali Linux系列教程之BeFF安装与集成Metasploit 文/玄魂 kali Linux系列教程之BeFF安装与集成Metasploit 1.1 apt-get安装方式 1.2 启动 1. ...
- Centos 6 搭建安装 Gitlab
官方安装教程 gitlab / gitlab-ce 官网下载:https://www.gitlab.cc/downloads 官网安装说明:https://doc.gitlab.cc/ce/insta ...
- redis集群离线安装环境搭建过程
本文是继上次redis集群重新整理的离线搭建环境,关于前期的redis集群准备工作参考我另一篇博客: http://www.cnblogs.com/qlqwjy/p/8566573.html 由于集群 ...
- 编译的 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 ...
随机推荐
- ASP.NET CORE 学习之自定义异常处理
为什么异常处理选择中间件? 传统的ASP.NET可以采用异常过滤器的方式处理异常,在ASP.NET CORE中,是以多个中间件连接而成的管道形式处理请求的,不过常用的五大过滤器得以保留,同样可以采用异 ...
- Python学习笔记010——形参与实参
在使用中忽略了一个问题,形参有些和实参类似,也不能是“关键字后面含有位置参数”,即“默认形参”后面必须不能含有“位置”形参! def test(a=100,b): print("test&q ...
- Python 之ConfigParser 学习笔记
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...
- 转 web前端性能分析--原理篇
转自http://blog.csdn.net/five3/article/details/7686715 web前端性能: 即是web用户在访问一个页面时所要花费的时间总和.即一个完全意义上的用户响应 ...
- 3dmax 物体的真正局部空间原点
假设在3dmax中创建一个 长x宽x高=1cm x 1cm x 1cm 的单位立方体,则默认局部坐标系原点在底面中心,进入 “层次”面板->轴->调整轴,按下“仅影响轴”,再点“居中到对象 ...
- 强者联盟——Python语言结合Spark框架
引言:Spark由AMPLab实验室开发,其本质是基于内存的高速迭代框架,"迭代"是机器学习最大的特点,因此很适合做机器学习. 得益于在数据科学中强大的表现,Python语言的粉丝 ...
- Linux操作系统及应用课程笔记 索引
第0部分 软件的安装与配置 Linux下软件的安装与配置 第1部分* 绪论 第2部分* Linux的安装过程 第3部分 系统Shell和经常使用命令 Shell文件相关经常使用命令及參数总 ...
- C++学习笔记16,C++11中的显式的默认构造函数以及显示删除默认构造函数
在早期的C++中.假设须要一些接受一些參数的构造函数,同一时候须要一个不接收不论什么參数的默认构造函数.就必须显示地编写空的默认构造函数.比如: //tc.h class A{ private: in ...
- 基于python的接口测试框架设计(三)接口测试的框架
基于python的接口测试框架设计(三)接口测试的框架 其实我这里用到的是unittest单元测试框架,,这个框架好就好在比较清楚,,setup terdown都可以处理一些初始化及完成后的工作 主要 ...
- ubuntu 文档查看器/gedit查看txt中文乱码问题
文档查看器界面是中文的,但查看pdf文档只显示英文,中文都空了出来. 用命令: sudo apt-get install poppler-data 解决该问题. gedit查看txt中文乱码问题 打开 ...