【问题标题】:Activemodel date validation and assignment in forms表单中的 Activemodel 日期验证和分配
【发布时间】:2013-09-01 16:10:47
【问题描述】:

我使用的是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变量?

【问题讨论】:

  • 不确定dob 是什么?
  • 这是如何工作的? dob(3i)
  • 我也有同样的问题,以防你找到答案。
  • 不,我想我没有找到答案,我转而使用活动记录。
  • @phil88530 你找到答案了吗?

标签: ruby-on-rails ruby validation date activemodel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多