我看过其他关于Mongoid和has_secure_password的问题,但我的情况有所不同,在创建“ActiveModel :: SecurePassword”做好工作时,创建的password_digest没有问题。但是当我尝试进行身份验证时,我收到错误:“未定义方法`身份验证'对于Mongoid :: Criteria:0x007fe2b0d99488 ”指向“ @ room.authenticate(params [:password] )“我的checkpoints_controller.rb:
def create
@room = Room.where(url: params[:room_url])
if @room && @room.authenticate(params[:password])
session[@room.id.to_s.to_sym] = true
redirect_to "/chat/" + @room.url
else
redirect_to "/checkpoint/" + @room.url, alert: "This room may not exists or your password is incorrect."
end
end
在我的样板房里.rb一切都很好:
class Room
include Mongoid::Document
include ActiveModel::SecurePassword
embeds_one :guest
embeds_many :messages
has_secure_password
field :password_digest
field :owner_name
field :url
end
我正在使用:Rails 4.0.0和Mongoid 4.0.0.beta1。
答案 0 :(得分:0)
替换此行
@room = Room.where(url: params[:room_url])
这一行
@room = Room.where(url: params[:room_url]).first
答案 1 :(得分:0)
我正在研究类似的情况。对我有用的是在字段password_digest之后移动has_secure_password。
class Room
include Mongoid::Document
include ActiveModel::SecurePassword
embeds_one :guest
embeds_many :messages
field :password_digest
has_secure_password
field :owner_name
field :url
end