代码之家  ›  专栏  ›  技术社区  ›  Steven Aguilar

未初始化常量ApplicationRecord

  •  18
  • Steven Aguilar  · 技术社区  · 8 年前

    我正在在线学习rails教程,当我转到 http://localhost:3000/

    “未初始化常量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>

    还有我的用户。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
    3 回复  |  直到 8 年前
        1
  •  29
  •   oreoluwa    8 年前

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

    立即修复:

    class User < ActiveRecord::Base
    ...
    end
    

        2
  •  21
  •   Paweł Gościcki Ywain    6 年前

    引用infured的答案 https://stackoverflow.com/a/41388844/5598043

    创建名为 app/models/application_record.rb 包含以下内容:

    class ApplicationRecord < ActiveRecord::Base
      self.abstract_class = true
    end
    
        3
  •  5
  •   user9869932    5 年前

    如果您在从Rails5迁移到Rails6之后得到了这个消息,请确保更改

    config.load_defaults 5.2
    

    对于

    config.load_defaults 6.0
    

    在您的 config/application.rb 文件