NoMethodError:未定义的方法`encrypted_pa​​ssword设计gem

时间:2013-12-24 05:52:08

标签: ruby-on-rails mysql2

this is my user model:


class User < Account
       devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


          attr_accessible :email, :password, :password_confirmation, :remember_me

       devise :database_authenticatable,
       :ga_otp_authenticatable,
    :yk_otp_authenticatable,
    :registerable,
    :confirmable,
    :recoverable,
    :trackable,
    :validatable,
    :lockable,
    :timeoutable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :password, :password_confirmation, :remember_me, :time_zone, :require_ga_otp, :require_yk_otp, :full_name, :address

  attr_accessor :captcha,
    :skip_captcha,
    :new_password,
    :new_password_confirmation,
    :current_password

  before_validation :generate_name,
    :on => :create

  has_many :trade_orders,
    :dependent => :destroy

  has_many :purchase_trades,
    :class_name => "Trade",
    :foreign_key => "buyer_id"

  has_many :sale_trades,
    :class_name => "Trade",
    :foreign_key => "seller_id"

  has_many :invoices,
    :dependent => :destroy

  has_many :yubikeys,
    :dependent => :destroy

  has_many :bank_accounts,
    :dependent => :destroy

  has_many :tickets,
    :dependent => :destroy

  #has_many :tickets,
  #  :dependent => :destroy

  validates :email,
    :uniqueness => true,
    :presence => true

  validate :captcha do
    if captcha.nil? and new_record?
      unless skip_captcha
        errors[:captcha] << I18n.t("errors.answer_incorrect")
      end
    end
  end

  def captcha_checked!
    self.captcha = true
  end

  def bitcoin_address
    super or (generate_new_address && super)
  end

  def qr_code
    if @qrcode.nil?
      @qrcode = RQRCode::QRCode.new(bitcoin_address, :size => 6)
    end
    @qrcode
  end

  def confirm!
    super
    UserMailer.registration_confirmation(self).deliver
  end

  protected

  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    name = conditions.delete(:name)
    where(conditions).where(["name = :value OR email = :value", { :value => name }]).first
  end

  def generate_name
    self.name = "BF-U#{"%06d" % (rand * 10 ** 6).to_i}"
  end
end

这是我的帐户模型:

     class Account < ActiveRecord::Base
  has_many :account_operations

  has_many :used_currencies,
    :dependent => :destroy

  belongs_to :parent,
    :class_name => 'Account'

  validates :name,
    :presence => true,
    :uniqueness => true

  # BigDecimal returned here
  def balance(currency, options = {} )
    account_operations.with_currency(currency).with_confirmations(options[:unconfirmed]).with_processed_active_deposits_only.map(&:amount).sum.round(5).abs
  end

  # Generates a new receiving address if it hasn't already been refreshed during the last hour
  def generate_new_address
    unless last_address_refresh && last_address_refresh > DateTime.now.advance(:hours => -1)
      self.last_address_refresh = DateTime.now
      self.bitcoin_address = Bitcoin::Client.instance.get_new_address(id.to_s)
      save
    end
  end

  def max_withdraw_for(currency)
    Transfer.round_amount(self.balance(currency), currency)
  end

  def self.storage_account_for(currency)
    account_name = "storage_for_#{currency.to_s.downcase}"
    account = find_by_name(account_name)

    if account
      account
    else
      Account.create! do |a|
        a.name = account_name
      end
    end
  end
end

我的问题是我正在尝试输入密码,但它不接受密码字段。密码不能为空,这是错误的。

我尝试手动创建用户。这里给出了错误的未定义方法加密密码。

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

试试这个...... 您的模型上是否有encrypted_pa​​ssword列?

attr_accessible :email, :password, :password_confirmation, :remember_me, :encrypted_password

仅仅因为它的attr_accessable并不意味着该属性存在,只是意味着你可以访问它(如果确实存在)。进入rails控制台并运行:

User.new.respond_to?(:encrypted_password=)

如果返回true,则表示模型中有列;如果不是,则需要确保运行正确的迁移。

User.new.respond_to?(:encrypted_password=) => false

I run rake db:migrate:reset and it work!!!
相关问题