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 ...
随机推荐
- RedisDesktopManager无法连接虚拟机中启动的redis服务问题解决
问题:开启虚拟机Redis,用RedisDesktopManager链接报超时. 问题排查: 1.检查redis是否正常启动 结果:正常启动 2.查看网络是否正常 结果:正常 3.检查redis bi ...
- [置顶] Android中使用Movie显示gif动态图
转载请注明: http://blog.csdn.net/u012975705/article/details/48717391 在看这篇博文之前对attr自定义属性还是不是很熟的童鞋可以先看看:An ...
- Python 字典 update() 方法
描述 Python 字典 update() 方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中. 用法与 Python dict() 函数相似. 语法 update ...
- cocos2dx 3.1从零学习(五)——动画
动画是游戏中最重要的表现部分,本篇仅仅是初步学习大部分动画的用法.没有什么原理性的东西,可是样例有非常多,假设有不熟的地方要练一下. 特别是Spawn和Sequence的组合使用,什么时候使用Spaw ...
- python dataframe 在merge时 产生笛卡尔积
在pandas中,concat, merge, join的使用方法可以参考以下资料: http://blog.csdn.net/stevenkwong/article/details/52528616 ...
- Linux中telnet命令
telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...
- laravel建立一个分组控制器和分组路由
路由 Route::group(['domain' => 'laravel.8g.com','namespace' => 'Admin'],function() { Route::get( ...
- 进程间通信(java)--队列
前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便 ...
- JavaScript:零星知识
1. 关于document.write() 如果在文档已完成加载后执行 document.write,整个HTML 页面将被覆盖. 2. 对代码行进行折行 您可以在文本字符串中使用反斜杠对代码行进行换 ...
- [Jobdu] 题目1369:字符串的排列
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入: 每个 ...