Ruby on rails 3 客户端验证自定义方法未定义
使用自定义验证器加载表单会生成NoMethodErrorRuby on rails 3 客户端验证自定义方法未定义,ruby-on-rails-3,client-side-validation,Ruby On Rails 3,Client Side Validation,使用自定义验证器加载表单会生成NoMethodError undefined method `validates_soggetti' 设置 gem 'rails', '3.2.13' gem 'client_side_validations', :github => 'bcardarella/client_side_validations', :branch => '3-2-stable' 初始值设定项已阻止未注释的附加邮件。 app/validators/soggetti_va
undefined method `validates_soggetti'
设置
gem 'rails', '3.2.13'
gem 'client_side_validations', :github => 'bcardarella/client_side_validations', :branch => '3-2-stable'
初始值设定项已阻止未注释的附加邮件。app/validators/soggetti_validator.rb包含
class SoggettiValidator < ActiveModel::EachValidator
def validate(record)
unless :quantita >= :soggetti
record.errors.add(:soggetti, :too_big)
end
end
module ActiveModel::Validations::HelperMethods
def validates_soggetti(*attr_names)
validates_with SoggettiValidator, _merge_attributes(attr_names)
end
end
end
只有自定义验证不会生效。我假设问题在于模块定义。通过在/liba validations\u helpers.rb下创建包含模块语句的文件来解决
module ActiveModel::Validations::HelperMethods
def validates_soggetti(*attr_names)
validates_with SoggettiValidator, _merge_attributes(attr_names)
end
end
并调用相关控制器中的模块
此外,模型还需要指定轨道3:
validates :height, :presence => true, :numericality => true
validates :width, :presence => true, :numericality => true
validates :quantita, :presence => true, :numericality => {:only_integer => true}
validates :soggetti, :soggetti => true, :numericality => {:only_integer => true}
这与上的说明略有不同
这表明验证了\u myCustomValidation
module ActiveModel::Validations::HelperMethods
def validates_soggetti(*attr_names)
validates_with SoggettiValidator, _merge_attributes(attr_names)
end
end
validates :height, :presence => true, :numericality => true
validates :width, :presence => true, :numericality => true
validates :quantita, :presence => true, :numericality => {:only_integer => true}
validates :soggetti, :soggetti => true, :numericality => {:only_integer => true}