我正在使用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变量?
答案 0 :(得分:2)
有一个Pull Request to Rails 4.2。您可以深入了解https://github.com/rails/rails/pull/8189/files。