Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在回形针中存储基于URL的图像_Ruby On Rails_Ruby_Ruby On Rails 4_Rubygems_Paperclip - Fatal编程技术网

Ruby on rails 在回形针中存储基于URL的图像

Ruby on rails 在回形针中存储基于URL的图像,ruby-on-rails,ruby,ruby-on-rails-4,rubygems,paperclip,Ruby On Rails,Ruby,Ruby On Rails 4,Rubygems,Paperclip,我正在rails应用程序中使用回形针作为个人资料图片上传功能。这对于将图像上传到配置文件的默认情况非常有效,但我想允许没有图片的用户从预先准备好的“库存”图像中选择一个 这些图像位于本地“我的资源”“图像”文件夹中。因此,在这些情况下,我希望能够在不实际上传图像的情况下将图像添加到EventImage对象中,而只是引用本地路径上的URL 我已经尝试了这篇文章的每一个答案:但似乎没有一个有效。我使用的是回形针版本回形针(4.3.1 37589f9) 当我尝试以下解决方案时: def photo_f

我正在rails应用程序中使用回形针作为个人资料图片上传功能。这对于将图像上传到配置文件的默认情况非常有效,但我想允许没有图片的用户从预先准备好的“库存”图像中选择一个

这些图像位于本地“我的资源”“图像”文件夹中。因此,在这些情况下,我希望能够在不实际上传图像的情况下将图像添加到EventImage对象中,而只是引用本地路径上的URL

我已经尝试了这篇文章的每一个答案:但似乎没有一个有效。我使用的是回形针版本
回形针(4.3.1 37589f9)

当我尝试以下解决方案时:

def photo_from_url(url)
    puts "we got:"+url
    Thread.new do
    self.photo = URI.parse(url)
    end
  end
它不会导致存储图像引用,并且无论我传递到该方法的图像的URL如何,当我这样做时,它都不会显示我的图像:
-相反,它会显示未定位或存储图像时的默认图像

我还必须将其放入一个新线程中,否则它会被绑定并阻塞/导致超时,这似乎是URI.parse的问题,而且图像最终无法通过验证,因为照片是“空的”,这在我的验证中是不允许的,因此我最终删除了:photo上的验证存在行,这仍然无法解决问题。我真的只想模型回形针方法:照片-指向一个本地网址有时,我的文件正常上传其他时间

请看这里的全班同学:

# == 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
#==架构信息
#
#表名:事件图像
#
#id:整数不为空,主键
#标题:字符串
#事件id:整数
#创建时间:datetime非空
#更新时间:datetime非空
#照片\文件\名称:字符串
#照片内容类型:字符串
#照片文件大小:整数
#照片更新时间:datetime
#
类EventImage[“image/jpg”、“image/jpeg”、“image/gif”、“image/png”]
使用AttachmentSizeValidator验证\u,:attributes=>:photo,:小于\u=>3.MB
属于:事件
def photo_from_url(url)
新做的
self.photo=URI.parse(url)
结束
结束
结束

相反,我决定最好在EventImage模型中添加“罐装图像id”,然后在模型中使用照片url方法,该方法可以选择返回回形针url或罐装图像url,具体取决于图像是否为罐装图像。它还将这种复杂性隐藏在助手方法后面:)