【问题标题】:Undefined method `authenticate' in Rails 4 Mongoid with has_secure_password带有has_secure_password的Rails 4 Mongoid中未定义的方法'authenticate'
【发布时间】:2014-03-04 23:29:52
【问题描述】:

我看过其他关于 Mongoid 和 has_secure_password 的问题,但我的情况不同,在创建“ActiveModel::SecurePassword”时做得很好,password_digest 的创建没有问题。但是,当我尝试进行身份验证时,我收到错误:“undefined method `authenticate' for 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。

【问题讨论】:

    标签: authentication ruby-on-rails-4 mongoid


    【解决方案1】:

    替换这一行

     @room = Room.where(url: params[:room_url])
    

    用这条线

     @room = Room.where(url: params[:room_url]).first
    

    【讨论】:

      【解决方案2】:

      我正在处理类似的情况。对我有用的是在字段 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多