custom validation without model in rails
I have created following class just to have validation and custom validation for parameters for my ajax message:
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_开发者_Python百科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
and getting following error inside "temp_outside_cannot_be_greater_than_temp_inside"
NoMethodError (undefined method `>' for :temp_outside:Symbol):
any idea?
You need to check the value of temp_outside, as it seems to be a symbol in this case, not a number
精彩评论