自定义的 validator 如下
class Distance < ActiveModel::Validator
def validate(record)
dis = record.store.address.distance_to(record.user.address.addr1)
if dis.to_s == 'NaN'
record.errors[:"user.address.addr1"] << "please enter a valid address"
elsif dis > record.store.delivery_radius
record.errors[:"user.address.addr1"] << "you are far away from this store"
end
end
end
验证都正常,但是 record.errors[:"user.address.addr1"] 这个 error message 就是不能正确显示。用了 simple_form 和 client_side_validations 插件。我觉得好象是 record.errors[:"user.address.addr1"] 这个地方写的有问题。。。
求指点!谢谢!