【问题标题】:rails 5 (Object does not support #inspect) when using rails consolerails 5(对象不支持#inspect)使用rails控制台时
【发布时间】:2017-01-20 02:19:24
【问题描述】:

我正在尝试在我的应用程序的 rails 控制台中初始化一个新用户。每当我输入初始化代码时:

irb(main):001:0> user = User.new

我得到这个错误:

(Object doesn't support #inspect)
=>
irb(main):002:0>

我已正确设置了用户凭据,并且没有以错误的方式使用任何代码。

class User
include Mongoid::Document

# User fields
field :first_name, type: String
field :last_name, type: String
field :phone_number, type: Integer
field :address, type: String
field :info, type: String
field :portfolio, type: String
field :motto, type: String
field :cover, type: BSON::Binary
field :avatar, type: BSON::Binary
field :photo, type: BSON::Binary
field :request, type: Boolean
field :decision, type: Boolean
field :gender, type: String
field :user_url, type: Mongoid::EncryptedHash
field :position, type: String
field :created_at, type: DateTime, default: ->{DateTime.now}
field :updated_at, type: DateTime
field :view_comments_count, type: Integer
field :thumbs_up_notifications, type: Boolean
field :tracking_notifications, type: Boolean
field :message_notifications, type: Boolean
field :applaud_notifications, type: Boolean
field :shout_out_notifications, type: Boolean
field :congrats_notifications, type: Boolean
field :post_notifications, type: Boolean


# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

## Database authenticatable
field :email,              type: String, default: ""
field :password, type: Mongoid::EncryptedString, default: ""

## Recoverable
field :reset_password_token,   type: Mongoid::EncryptedString
field :reset_password_sent_at, type: Time

## Rememberable
field :remember_created_at, type: Time

## Trackable
field :sign_in_count,      type: Integer, default: 0
field :current_sign_in_at, type: Time
field :last_sign_in_at,    type: Time
field :current_sign_in_ip, type: String
field :last_sign_in_ip,    type: String

## Confirmable
field :confirmation_token,   type: String
field :confirmed_at,         type: Time
field :confirmation_sent_at, type: Time
field :unconfirmed_email,    type: String # Only if using reconfirmable

## Lockable
field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is      :failed_attempts
field :unlock_token,    type: String # Only if unlock strategy is :email or :both
field :locked_at,       type: Time

attr_accessor :verify_password, :password_confirmation

attr_reader :comment_notifications, :post_notifications,
          :shout_out_notifications, :thumbs_up_notifications,
          :applaud_notifications, :tracking_notifications,
          :congrats_notifications, :message_notifications,
          :request_notifications, :acceptance_notifications


validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :password, presence: true
validates :phone_number, presence: true
validates :address, presence: true
validates :info, presence: true
validates :motto, presence: true
validates :gender, presence: true

validates_confirmation_of :password

has_many :posts
has_many :comments
has_many :thumbs_ups
has_many :congrats
has_many :tracks
has_many :shout_outs
has_many :applauds
has_many :assignments
has_one :position
has_one :rank
belongs_to :group


after_create :find_default_contacts, :create_url

before_save :validates_email, :validates_motto, :validates_info

如果有人能指出导致错误的原因,我将不胜感激。

【问题讨论】:

  • 你能给我看看你的用户迁移文件吗?只需从架构中复制和粘贴?
  • 我正在使用 NoSQL 数据库:MongoDB。没有迁移。
  • 你能粘贴完整的 user.rb 代码和你的 gemfile 吗?
  • 能否请您粘贴您的User 模型我认为您正在覆盖initialize 方法如果是,那么您应该在您的initialize 方法上调用super
  • 实际上没有初始化方法。这可能是错误一直显示的原因吗?

标签: ruby-on-rails irb rails-console


【解决方案1】:

User 对象的实例化及其对user 变量的赋值似乎工作得很好,所以我希望你确实有一个新的User 实例存储在user 中。尽管您包含在 User 类中的某些代码似乎未定义 User#inspect 方法。

Ruby 中的所有类,包括您的User 类,都继承自Object 类,并且Object 定义了一个名为inspect 的实例方法。 #inspect 通常调用#to_s 来返回对象的字符串表示形式。 IRB 使用此方法来生成如果该方法已定义,您通常会看到的输出。

您应该能够看到自己定义#inspect 是如何工作的:

class User
  def inspect
    "custom implementation of #inspect"
  end
end


irb(main):001:0> user = User.new
=> custom implementation of #inspect
irb(main):002:0>

因此,如果您愿意,可以提供自己的 #inspect,但奇怪的是您需要这样做。如果你想追踪这个,我会查看你包含在User(显然只是Mongoid::Document)中的代码,并尝试找出#inspect 未定义的位置和原因。

【讨论】:

  • 我没有看到任何可以使 #inspect 未定义的方法?
  • 刚刚发现文件中的几个方法导致错误。但我不知道是哪些。有没有办法识别它们。
  • 我也有同样的情况,但我的模型中有这段代码class User < ApplicationRecord enum role: [:seller, :buyer, :admin] devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable after_initialize :set_default_role, :if => :new_record? def set_default_role self.role ||= 'seller' end def name (self.username || self.email.split('@').first).humanize end 当我注释掉设计的东西时它工作得很好,也许设计挂钩中的某些模块缺少#inspect 方法
  • @edurante 我定义了检查方法,它只是打印出自定义文本,但没有产生我期望从User.all 或模型上的任何其他查询得到的正确结果。
【解决方案2】:

我发现我像这样在routes.rb 中使用了自定义设计path_names

devise_for :users, path_names: { sign_in: 'login', sign_out: 'logout' }

当我删除路径名时,错误消失了。

希望有一天这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多