Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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 无法在rails中显示远程图像_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 无法在rails中显示远程图像

Ruby on rails 无法在rails中显示远程图像,ruby-on-rails,ruby,Ruby On Rails,Ruby,我用载波上传图像。但是使用image\u uploader.rb文件中的默认store\u dir,它会将store\u dir附加到我的图像路径中。因此,我成功地显示了我上传的图像。但是,我有一个数据库,其中包含已经存在的远程图像URL。这些远程图像URL不显示,因为它将存储目录附加到图像路径,并且找不到它们 例如: 它将“”作为“/uploads/product/productimage/1/http%3A/myapp.com/images/I/51oYEfb%252B0WL.SL160.j

我用载波上传图像。但是使用image\u uploader.rb文件中的默认store\u dir,它会将store\u dir附加到我的图像路径中。因此,我成功地显示了我上传的图像。但是,我有一个数据库,其中包含已经存在的远程图像URL。这些远程图像URL不显示,因为它将存储目录附加到图像路径,并且找不到它们

例如: 它将“”作为“/uploads/product/productimage/1/http%3A/myapp.com/images/I/51oYEfb%252B0WL.SL160.jpg

这是我的密码:

_product.html.erb

<% @products.each do |product| %>
  <li> 
    <%= image_tag(product.productimage_url) if product.productimage? %>
  </li>
<% end %>

我假设您一定已经将远程URL加载到了
产品
”表的
产品图像
列中

也许实现目标的最简单方法是在products表中添加类似于
remote\u url
列的内容,而不是将远程url放在
productimage
列中。然后你可以做一些类似的事情:

Class Product < ActiveRecord::Base
  def image_url
    productimage.present? ? productimage_url : remote_url
  end
end
Product.all.each do |product|
  temp_location = Rails.root.join('tmp', File.basename(product.attributes['productimage']))
  uri = URI(product.attributes['productimage'])

  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new uri

    http.request request do |response|
      File.open(temp_location, 'w') do |file|
        response.read_body do |chunk|
          file.write chunk
        end
      end
    end
  end

  product.productimage = File.open(temp_location)
  product.save!

  File.unlink(temp_location)
end

我没有这方面的代码,但您的问题似乎是Carrierwave无法确定url是本地的还是远程的。如果是本地的,则加载正常(请记住,系统中的图像存储在某个地方)-远程必须以某种方式表示。是的,我在远程url中使用了另一列,正如@ihaztehcodez在下面的答案中指定的那样。谢谢。没问题,谢谢你的更新!我已经在我的表中添加了一个remote_url列,对我来说效果很好。谢谢但我有一个问题,有没有办法通过对字符串执行切片操作从“/uploads/product/productimage/1/http%3A/myapp.com/images/I/51oYEfb%252B0WL.SL160.jpg”提取http url?我的意思是,如果它在字符串片段中有http,则使用原始url。我试过了,但没用。我们可以这样做吗?还是不可能@ihaztehcodez@suma-chaganti您可以使用正则表达式(
/(http.*$)/
然后远程url位于
$1
)中,但这有点不规范,如果用户上载文件名中带有“http”的文件,就会中断。
def store_dir
  nil
end
Class Product < ActiveRecord::Base
  def image_url
    productimage.present? ? productimage_url : remote_url
  end
end
<%= image_tag(product.image_url) if product.image_url.present? %>
Product.all.each do |product|
  temp_location = Rails.root.join('tmp', File.basename(product.attributes['productimage']))
  uri = URI(product.attributes['productimage'])

  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new uri

    http.request request do |response|
      File.open(temp_location, 'w') do |file|
        response.read_body do |chunk|
          file.write chunk
        end
      end
    end
  end

  product.productimage = File.open(temp_location)
  product.save!

  File.unlink(temp_location)
end