我正在尝试在我的应用的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
如果有人可以指出导致错误的原因,我将不胜感激。
答案 0 :(得分:0)
User
对象的实例化及其对user
变量的赋值似乎工作得很好,所以我希望你确实有一个新的User
实例存储在user
。虽然您在User
课程中包含的某些代码未定义User
的{{1}}方法,但似乎会出现。
Ruby中的所有类(包括您的#inspect
类)都继承自User
类,Object
定义了一个名为Object
的实例方法。 inspect
通常调用#inspect
来返回对象的字符串表示形式。 IRB使用此方法生成在方法定义时通常会看到的输出。
您应该能够看到自己如何定义#to_s
:
#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
未定义的位置和原因。
答案 1 :(得分:0)
我发现自己在Auth::user()
中使用了这样的自定义设计path_names
routes.rb
当我删除路径名时,错误消失了。
希望有一天能对某人有所帮助。