新手问题 关于 authenticate 的疑问

u1453357893 · 2016年05月12日 · 最后由 tvallday 回复于 2016年05月12日 · 2213 次阅读

写了个登录功能:(输入手机号和密码实现登录功能)

登录方法如下:

def login_session
  customer = Customer.where(cus_phone: params[:cus_phone])
  if customer && customer.authenticate(params[:cus_password])
    session[:cus_id] = customer.id
    params[:session][:remember_me] == '1' ? remember(customer) : forget(customer)
    redirect_to customers_path
  else
    render :sign_in
  end
end

gemfile 里边儿已经加了:bcrypt-ruby 这个 gem

model 里边儿也 include 了 SecurePassword 指明一点我用的是 mongoid 没用 ActiveRecord

include ActiveModel::SecurePassword

 has_secure_password

但是现在的问题一直在报 undefined method `authenticate' 求大神指点

customer = Customer.find_by(cus_phone: params[:cus_phone])

把 customer 的 class 打印出来啊,没记错的话,where 返回的是一个可迭代对象,你应该使用 where(xxx: yyy).first 或者 find_by(xxx: yyy)

#2 楼 @watraludru 好的,知道了,犯了一个低级错误

#1 楼 @rei 当检索到对象之后调用 authenticate 这个 api 的时候出现了这个问题

customer.authenticate(params[:session][:cus_password])

@watraludru 为什么会出现上述问题呢

server 重启了吗?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号