1. 如何正确验证照片 URL?
您可以使用验证 URL 格式的插件或自己编写:
validates_each :photo_url do |record, attr, value|
begin
uri = URI::parse(value)
record.errors.add(nil, 'Sorry, you may only use http/https links') if (uri.class.to_s =~ /URI::HTTPS?/).nil?
record.errors.add(nil, 'The url must point to a picture') unless value =~ /\.(png|jpg|jpeg)$/i
rescue URI::InvalidURIError
record.errors.add(nil, 'The format of the url is not valid')
end
end
2. 安全问题?跨站脚本?
只要您对文本进行转义,就没有任何突出的安全问题。
<%=h image_tag obj.photo_url %>
是安全的。请记住,用户仍然可以使用 100MB 的图像,这会减慢每个访问者的速度。
3. 可能有免费的带有 API 的资产商店?
我不知道,但是 rackspace cloud,amazon s3 托管非常便宜。一些图片上传插件支持这两个,所以你至少可以节省一些时间。