简体   繁体   English

未初始化的常量ApplicationRecord

[英]uninitialized constant ApplicationRecord

I am working on the rails tutorial book online and I am getting the following error message when I go to http://localhost:3000/ 我正在线上的rails教程书,当我转到http:// localhost:3000 /时,我收到以下错误消息

"uninitialized constant ApplicationRecord" “未初始化的常量ApplicationRecord”

and it gives me the following code highlighting the first line. 它给了我以下代码突出显示第一行。

 class User < ApplicationRecord attr_accessor :remember_token before_save { self.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\\A[\\w+\\-.]+@[az\\d\\-.]+\\.[az]+\\z/i validates :email, presence: true, length: { maximum: 255 }, 

Here is my application.html.erb file: 这是我的application.html.erb文件:

 <!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> <%= render 'layouts/shim' %> </head> <body> <%= render 'layouts/header' %> <div class="container"> <% flash.each do |message_type, message| %> <div class="alert alert-<%= message_type %>"><%= message %></div> <% end %> <%= yield %> <%= render 'layouts/footer' %> <%= debug(params) if Rails.env.development? %> </div> </body> </html> 

And my user.rb file : 和我的user.rb文件:

 class User < ApplicationRecord attr_accessor :remember_token before_save { self.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\\A[\\w+\\-.]+@[az\\d\\-.]+\\.[az]+\\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 } # Returns the hash digest of the given string. def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end # Returns a random token. def User.new_token SecureRandom.urlsafe_base64 end # Remembers a user in the database for use in persistent sessions. def remember self.remember_token = User.new_token update_attribute(:remember_digest, User.digest(remember_token)) end # Returns true if the given token matches the digest. def authenticated?(remember_token) return false if remember_digest.nil? BCrypt::Password.new(remember_digest).is_password?(remember_token) end # Forgets a user. def forget update_attribute(:remember_digest, nil) end end 

It appears you're using the Rails 5 tutorial, but working with Rails 4. In Rails 5 all models inherit from ApplicationRecord , while Rails 4 from ActiveRecord::Base 看来你正在使用Rails 5教程,但是使用Rails 4.在Rails 5中,所有模型都继承自ApplicationRecord ,而Rails 4则来自ActiveRecord::Base

Immediate fix: 立即修复:

class User < ActiveRecord::Base
...
end

Long term fix, switch to Rails 5 and learn with Rails 5 长期修复,切换到Rails 5并学习Rails 5

Referring to infused's answer from https://stackoverflow.com/a/41388844/5598043 参考https://stackoverflow.com/a/41388844/5598043的注入答案

Create a new file called app/models/application_record.rb with the following content: 使用以下内容创建名为app/models/application_record.rb的新文件:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

If you are getting this after moving from Rails 5 to Rails 6, ensure you change 如果从Rails 5转移到Rails 6后得到这个,请确保更改

config.load_defaults 5.2

for 对于

config.load_defaults 6.0

in your config/application.rb file. 在您的config/application.rb文件中。

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

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