Rails:NameError:未初始化的常量 [英] Rails: NameError: uninitialized constant

查看:60
本文介绍了Rails:NameError:未初始化的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 PhoneNumber 的简单模型:

I have a simple model called PhoneNumber:

class PhoneNumber < ActiveRecord::Base
  validates :pnumber, presence: true, on: :create #=> { :message => " cannot be blank" }
  validates :pnumber, numericality: true, on: :create
end

我转到应用程序的根文件夹(包含应用程序子文件夹的文件夹)并启动控制台:

I go to the root folder of the application (the one containing the app sub-folder) and start the console:

rails console --sandbox

当我尝试创建一个空电话号码时(我想收到一条错误消息,因为验证将失败)我收到以下错误消息:

When I try to create an empty PhoneNumber (I want to get an error message as the validation shall fail) I am getting the following error message:

2.0.0-p451 :001 > PhoneNumber.new
NameError: uninitialized constant PhoneNumber
from (irb):1
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:43:in `require'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:43:in `block in exec_app_rails'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:32:in `loop'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:32:in `exec_app_rails'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/cli.rb:5:in `<top (required)>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/bin/rails:9:in `require'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/bin/rails:9:in `<top (required)>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rails:23:in `load'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rails:23:in `<main>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/2.2.2.02.02.02.0.2.2.02.222222.2.02.02.0.2.2.022222222222222

控制台似乎不知道该模型.在普通的 ruby​​ 中,您需要要求"包含该类的文件,但我认为 rails 控制台应自动加载所有模型.这是怎么回事?

It seems the console is not aware of the model. In plain ruby you need to 'require' the file containing the class but I thought that the rails console shall automatically load all models. What is going on here?

推荐答案

一些尝试:

  1. 重启rails控制台;如果您执行 > 操作,则对模型的更改只会被已经打开的 rails 控制台接收到.重新加载!(虽然我发现这是不可预测的),或者通过重新启动控制台.

  1. Restart the rails console; changes to your models will only get picked up by a rails console that is already open if you do > reload! (although I have found this to be unpredictable), or by restarting the console.

您的模型文件是否名为phone_number.rb"并且在/app/models"中?

Is your model file called "phone_number.rb" and is it in "/app/models"?

您应该仔细检查 rails 控制台命令中的--sandbox"选项.AFAIK,这会阻止更改.不用开关试试.

You should double-check the "--sandbox" option on your rails console command. AFAIK, this prevents changes. Try it without the switch.

这篇关于Rails:NameError:未初始化的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆