在rails中没有模型的自定义验证

时间:2011-08-20 12:40:20

标签: ruby-on-rails validation

我创建了以下类只是为了对我的ajax消息的参数进行验证和自定义验证:

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

  attr_accessor :attributes

  validates_inclusion_of :temp_inside, :in => 0..30
  validates_inclusion_of :temp_outside, :in => -50..20
  validates_presence_of :isol
  validates_inclusion_of :height, :length, :width, :in => 1..500
  validate :temp_outside_cannot_be_greater_than_temp_inside 

  def temp_outside_cannot_be_greater_than_temp_inside  
     errors.add(:temp_outside, "can't be greater than total value") if
        temp_outside > temp_inside
  end

  def initialize(attributes = {})
    @attributes = attributes 
  end

  def read_attribute_for_validation(key)
    @attributes[key]
  end

  def persisted?  
    false  
  end
end

并在" temp_outside_cannot_be_greater_than_temp_inside"

内获得关注错误
NoMethodError (undefined method `>' for :temp_outside:Symbol):

任何想法?

1 个答案:

答案 0 :(得分:0)

你需要检查temp_outside的值,因为在这种情况下它似乎是一个符号,而不是一个数字