ruby 代码
- validates_each :href, :on => :create do |record, attr, value|
- begin
- uri = URI.parse(value)
- if uri.class != URI::HTTP
- record.errors.add(attr, 'Only HTTP protocol addresses can be used')
- end
- rescue URI::InvalidURIError
- record.errors.add(attr, 'The format of the url is not valid.')
- end
- end
本文介绍了一个使用Ruby实现的模型验证方法,确保URL字段只接受有效的HTTP协议地址,并检查URL格式的有效性。
628

被折叠的 条评论
为什么被折叠?



