繁体   English   中英

Ruby on Rails-NameError:未初始化的常数User

[英]Ruby on Rails - NameError: uninitialized constant User

一段时间没有使用Rails了,只是克隆了一个项目,尝试使用数据库,但是我遇到了这个未初始化的常量错误,我似乎不知道为什么。 我捆绑安装,创建数据库,迁移并植入了一些文件,但似乎找不到问题的根源。 这不是一个小项目,当我使用它时似乎工作得很好……任何帮助将不胜感激……

这是我的gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.1'
gem 'httparty'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
# gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt'
gem 'semantic-ui-sass', git: 'https://github.com/doabit/semantic-ui-sass.git'
# gem 'capistrano-rails', group: :development
gem 'pg_search'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'faker'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end
gem 'rails_12factor', group: :production
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

这是用户模型:

class User < ApplicationRecord
  validates :first_name, :last_name, :presence => true
  validates :username, :email, :presence => true, :uniqueness => true
  has_many :messages
  has_many :posts, :foreign_key => :creator_id
  has_many :apprenticeships, :foreign_key => :requestor_id
  has_many :skills, through: :posts

  has_secure_password

  def full_name
    "#{self.first_name} #{self.last_name}"
  end

end

我也尝试过检查是否使用ActiveRecord::Base.connection.tables创建表,但是我仍然收到NameError: uninitialized constant ActiveRecord

在Ruby on Rails项目中工作时,请使用rails console而不是irbpry Rails的引导过程很复杂,并且许多类都是延迟加载的。 通用交互式控制台(如irbpry不会执行必要的引导程序来加载Rails应用程序,但rails console会执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM