在 Paperclip 中仅基于 URL 存储图像
Storing an image based upon nothing but a URL in Paperclip
我在我的 rails 应用程序中使用回形针作为个人资料图片上传功能。这对于将图片上传到配置文件的默认情况非常有效,但我想让没有图片的用户从一组预制 'stock' 图片中挑选一张。
这些图像在我的资产图像文件夹中本地托管。因此,在这些情况下,我希望能够在不实际上传图像的情况下将图像添加到我的 EventImage 对象,更多只是在本地路径中引用 URL。
我几乎尝试了这个 post 的所有答案:Save image from URL by paperclip 但其中 none 似乎有效。我正在使用回形针版本 paperclip (4.3.1 37589f9)
当我尝试以下解决方案时:
def photo_from_url(url)
puts "we got:"+url
Thread.new do
self.photo = URI.parse(url)
end
end
它导致没有图像参考被存储,并且不管我传递给该方法的图像的 URL,当我这样做时它从不显示我的图像:<%= image_tag @event.event_images.first.photo.url %>
- 相反它显示未找到或存储图像时的默认图像。
我还必须将它放在一个新线程中,否则它会被捆绑并阻塞/导致超时,这似乎是 URI.parse 的一个问题,而且图像最终验证失败,因为照片是'empty' 这在我的验证中是不允许的,所以我最终删除了 :photo 上的验证存在行,这仍然没有解决问题。我真的只希望模型回形针方法 :photo - 有时指向本地 url,而其他时候我正确地正常上传文件。
在此处查看全部 class:
# == Schema Information
#
# Table name: event_images
#
# id :integer not null, primary key
# caption :string
# event_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# photo_file_name :string
# photo_content_type :string
# photo_file_size :integer
# photo_updated_at :datetime
#
class EventImage < ActiveRecord::Base
attr_accessor :PAPERCLIP_STORAGE_OPTS
has_attached_file :photo , PAPERCLIP_STORAGE_OPTS
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"]
validates_with AttachmentSizeValidator, :attributes => :photo, :less_than => 3.megabytes
belongs_to :event
def photo_from_url(url)
Thread.new do
self.photo = URI.parse(url)
end
end
end
相反,我决定最好将 'canned_image_id' 添加到 EventImage 模型,然后在模型中使用 photo_url 方法,该方法可以选择 return回形针 url 或罐装图像 url 取决于图像是否是罐装图像。它还隐藏了辅助方法背后的这种复杂性:)
我在我的 rails 应用程序中使用回形针作为个人资料图片上传功能。这对于将图片上传到配置文件的默认情况非常有效,但我想让没有图片的用户从一组预制 'stock' 图片中挑选一张。
这些图像在我的资产图像文件夹中本地托管。因此,在这些情况下,我希望能够在不实际上传图像的情况下将图像添加到我的 EventImage 对象,更多只是在本地路径中引用 URL。
我几乎尝试了这个 post 的所有答案:Save image from URL by paperclip 但其中 none 似乎有效。我正在使用回形针版本 paperclip (4.3.1 37589f9)
当我尝试以下解决方案时:
def photo_from_url(url)
puts "we got:"+url
Thread.new do
self.photo = URI.parse(url)
end
end
它导致没有图像参考被存储,并且不管我传递给该方法的图像的 URL,当我这样做时它从不显示我的图像:<%= image_tag @event.event_images.first.photo.url %>
- 相反它显示未找到或存储图像时的默认图像。
我还必须将它放在一个新线程中,否则它会被捆绑并阻塞/导致超时,这似乎是 URI.parse 的一个问题,而且图像最终验证失败,因为照片是'empty' 这在我的验证中是不允许的,所以我最终删除了 :photo 上的验证存在行,这仍然没有解决问题。我真的只希望模型回形针方法 :photo - 有时指向本地 url,而其他时候我正确地正常上传文件。
在此处查看全部 class:
# == Schema Information
#
# Table name: event_images
#
# id :integer not null, primary key
# caption :string
# event_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# photo_file_name :string
# photo_content_type :string
# photo_file_size :integer
# photo_updated_at :datetime
#
class EventImage < ActiveRecord::Base
attr_accessor :PAPERCLIP_STORAGE_OPTS
has_attached_file :photo , PAPERCLIP_STORAGE_OPTS
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"]
validates_with AttachmentSizeValidator, :attributes => :photo, :less_than => 3.megabytes
belongs_to :event
def photo_from_url(url)
Thread.new do
self.photo = URI.parse(url)
end
end
end
相反,我决定最好将 'canned_image_id' 添加到 EventImage 模型,然后在模型中使用 photo_url 方法,该方法可以选择 return回形针 url 或罐装图像 url 取决于图像是否是罐装图像。它还隐藏了辅助方法背后的这种复杂性:)