2

我正在使用activemodel(不是activerecord,并且我有以下代码:

class PaymentForm
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming 

  attr_accessor :agree_auto_renew, :card_owner, :address1, :address2, :address3, :address4, :postcode,
                :firstname, :surname, :dob, :email, :email_confirmation
  validates_presence_of :agree_auto_renew, :card_owner, :address1, :address2, :address3, :address4, :postcode
  validates_presence_of :firstname, :surname, :email, :email_confirmation, :if => :not_owner
  validates_date :dob, :if => :not_owner
  validates_confirmation_of  :email, :if => :not_owner

  private
  def initialize
    @errors = ActiveModel::Errors.new(self)
  end

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def not_owner
    ! self.card_owner
  end

  def persisted?
    false
  end

end

但是当我尝试验证表单时,我在日期上收到以下 rails 错误:

undefined method `dob(3i)=' for #<PaymentForm:0x007fe0379c3668>

如何使 activemodel 自动将 dob 元素转换为 dob 变量?

4

1 回答 1

2

为此,Rails 4.2 有一个拉取请求。您可以深入了解https://github.com/rails/rails/pull/8189/files

于 2014-11-26T16:08:04.993 回答