未初始化常量ApplicationRecord。

20
我正在学习在线的 Rails 教程书,并在访问 http://localhost:3000/ 时遇到以下错误信息:

"uninitialized constant ApplicationRecord"

同时,它给了我下面这段代码并将第一行标记出来。

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+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },

以下是我的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>

我的 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+\-.]+@[a-z\d\-.]+\.[a-z]+\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


2
你可以检查一下 app/models/application_record.rb 是否存在吗? - oreoluwa
我没有application_record.rb文件,我需要有一个吗? - Steven Aguilar
应用程序记录仅适用于Rails 5。您正在运行Rails 5吗? - max pleaner
不,我没有运行Rails 5,我正在运行Rails 4。 - Steven Aguilar
那就是我碰到的问题,我在运行 Rails 4。 - Steven Aguilar
3个回答

32

看起来您正在使用 Rails 5 教程,但是在处理 Rails 4。在 Rails 5 中,所有模型都继承自 ApplicationRecord,而 Rails 4 继承自 ActiveRecord::Base

立即解决办法:

class User < ActiveRecord::Base
...
end

长期解决方案,切换到Rails 5并学习使用Rails 5


23

参考来自infused在https://stackoverflow.com/a/41388844/5598043的回答。

创建一个名为app/models/application_record.rb的新文件,并使用以下内容:

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

6
如果您在从Rails 5迁移到Rails 6后遇到此问题,请确保更改。
config.load_defaults 5.2

对于

config.load_defaults 6.0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接