在Rails 3 ActiveModel中获得自定义验证错误

问题描述

| 我正在尝试使用日期验证器宝石,但遇到错误,我不确定是否是因为模型不是Active Record(我见过有人建议不在ActiveRecord中时,验证在ActiveModel中有点时髦。 我正在使用Ruby 1.9.2和Rails 3.0.7。我已经附上了课程和下面的错误。 在此先感谢您的帮助! 类
require \'ice_cube\'
require \'active_support\'
require \'active_support/time_with_zone\'
require \'ostruct\'
require \'tzinfo\'
require \'active_model\'
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
class Schedule
  attr_accessor :yaml,:repeat,:interval,:interval_unit,:start_time,:start_date,:end_date,:end_time,:ends,:on,:after,:monday,:tuesday,:wednesday,:thursday,:friday,:saturday,:sunday
  validates_presence_of :repeat,:ends
  validates_presence_of :interval,:if => :ends_never? 
  validates_presence_of :on,:if => :ends_on_date?
  validates_numericality_of :after,:greater_than => 0,:if => :ends_after_recurrences? 
  validates :start_date,:date => { :after => Time.now }
  validates :end_date,:date => { :after => :start_date }
  validates :on,:date => { :after => :end_date } 

  def initialize(attributes = {})
  end

  def persisted?
    false
  end

  private
  def parse_schedule
    if :repeat == 0
      get_repeatable
    end

  end
  def parse_yaml
  end

  def ends_on_date?
    return :ends == \"on\"
  end
  def ends_never?
    return :ends == \"never\"
  end
  def ends_after_recurrences?
    return :ends == \"after\"
  end
end
来自Rails Console的错误
ruby-1.9.2-p180 :001 > s = Schedule.new
NoMethodError: undefined method `new\' for DateValidator:Module
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/with.rb:70:in `block in validates_with\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/with.rb:69:in `each\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/with.rb:69:in `validates_with\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/validates.rb:90:in `block in validates\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/validates.rb:83:in `each\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/validates.rb:83:in `validates\'
    from /Users/chance/Sites/EatingNow/app/models/schedule.rb:16:in `<class:Schedule>\'
    from /Users/chance/Sites/EatingNow/app/models/schedule.rb:10:in `<top (required)>\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:454:in `load\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:454:in `block in load_file\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:453:in `load_file\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:340:in `require_or_load\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:491:in `load_missing_constant\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:183:in `block in const_missing\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `each\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `const_missing\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in `const_missing\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/rspec-expectations-2.5.0/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing\'
    from (irb):1
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start\'
    from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>\'
    from script/rails:6:in `require\'
    from script/rails:6:in `<main>\'
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)