gpt4 book ai didi

ruby-on-rails - rails : NameError: uninitialized constant

转载 作者:行者123 更新时间:2023-12-03 09:00:04 25 4
gpt4 key购买 nike

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

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

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

当我尝试创建一个空的 PhoneNumber 时(我想收到一条错误消息,因为验证将失败)我收到以下错误消息:
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 控制台应自动加载所有模型。这里发生了什么?

最佳答案

一些尝试:

  • 重启 rails 控制台;如果您这样做 > reload! 对模型的更改只会被已经打开的 rails 控制台接收到(虽然我发现这是不可预测的),或者通过重新启动控制台。
  • 您的模型文件是否名为“phone_number.rb”并且在“/app/models”中?
  • 您应该仔细检查 rails 控制台命令中的“--sandbox”选项。 AFAIK,这会阻止更改。不用开关试试。
  • 关于ruby-on-rails - rails : NameError: uninitialized constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393445/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com