Ruby on rails 客户端验证未定义的方法`验证电子邮件';
我从客户端验证页面复制了代码。我犯了错误Ruby on rails 客户端验证未定义的方法`验证电子邮件';,ruby-on-rails,client-side-validation,Ruby On Rails,Client Side Validation,我从客户端验证页面复制了代码。我犯了错误 undefined方法验证电子邮件的有效性 当我在模型中放入includes::Validations时,这个错误消失了,但验证根本不起作用 app/validators/email_validator.rb class EmailValidator < ActiveModel::EachValidator def validate_each(record, attr_name, value) unless value =~ /^([^
undefined方法验证电子邮件的有效性
当我在模型中放入includes::Validations
时,这个错误消失了,但验证根本不起作用
app/validators/email_validator.rb
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attr_name, value)
unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
record.errors.add(attr_name, :email, options.merge(:value => value))
end
end
end
# This allows us to assign the validator in the model
module ActiveModel::Validations::HelperMethods
def validates_email(*attr_names)
validates_with EmailValidator, _merge_attributes(attr_names)
end
end
app/assets/javascripts/rails.validations.customValidators.js
// The validator variable is a JSON Object
// The selector variable is a jQuery Object
window.ClientSideValidations.validators.local['email'] = function(element, options) {
// Your validator code goes in here
if (!/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test(element.val())) {
// When the value fails to pass validation you need to return the error message.
// It can be derived from validator.message
return options.message;
}
}
models/comment.rb
class Comment < ActiveRecord::Base
attr_accessible :content, :email, :ip, :name, :post_id
belongs_to :post
validates_presence_of :content, :email, :post_id, :name
validates_email :email
end
class注释
我看不到您包含模块。此外,根据您放置模块的位置,您有时需要“要求”模块。此外,请尝试:
include <Module name>
包括
而不是
includes <Module name>
包括
希望这会有所帮助
includes <Module name>