博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rails NameError uninitialized constant class solution
阅读量:7229 次
发布时间:2019-06-29

本文共 2798 字,大约阅读时间需要 9 分钟。

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. Read to know more about configurations.

转载地址:http://ukdfm.baihongyu.com/

你可能感兴趣的文章
dubbo与springcloud初识
查看>>
iis web.config 配置示例
查看>>
归并排序
查看>>
java 的转义字符
查看>>
SharedPreferences的使用注意事项
查看>>
sofa-pbrpc高级用法
查看>>
Oracle 函数返回表实例2种写法实例
查看>>
mysql数据库主从复制
查看>>
Shell标准输出、标准错误 >/dev/null 2>&1
查看>>
Android自定义对话框(Dialog)位置,大小
查看>>
设置python的默认编码为utf8
查看>>
简易sqlhelper-java
查看>>
通过案例对SparkStreaming 透彻理解三板斧之一:解密SparkStreaming运行机制
查看>>
HBuilder 学习笔记
查看>>
利用OpenStreetMap(OSM)数据搭建一个地图服务
查看>>
TopN算法与排行榜
查看>>
lucene排序算法之向量空间模型(一)
查看>>
新浪微博数据Json格式解析
查看>>
WLAN 802.11 wifl区别
查看>>
oracle授权动态视图权限给用户
查看>>