rails 5(对象不支持#inspect)



我正在尝试在应用程序的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

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

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在哪里以及为什么未定义。

我发现我在routes.rb中使用了像这个一样的自定义设备path_names

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

当我删除path_names时,这个错误就消失了。

希望这有一天能帮助到别人。

最新更新