ruby - Unable to display remote images in rails -
i using carrier wave upload images. default store_dir in image_uploader.rb file, appending store_dir image path. successful in displaying images have uploaded. however, have database remote image urls existing. these remote image urls not displayed, appending store_dir image path , not found.
for eg: taking "http://myapp.com/images/i/51oyefb%2b0wl.sl160.jpg" "/uploads/product/productimage/1/http%3a/myapp.com/images/i/51oyefb%252b0wl.sl160.jpg"
here code:
_product.html.erb
<% @products.each |product| %> <li> <%= image_tag(product.productimage_url) if product.productimage? %> </li> <% end %>
product.rb
class product < activerecord::base mount_uploader :productimage, productimageuploader end
productimage_uploader.rb
class productimageuploader < carrierwave::uploader::base include carrierwave::minimagick storage :file def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end end
i tried nil below , still appends / image url:
def store_dir nil end
i'm assuming must have loaded remote urls products
' table's productimage
column.
perhaps simplest way accomplish goal add remote_url
column products table , not put remote urls in productimage
column. like:
class product < activerecord::base def image_url productimage.present? ? productimage_url : remote_url end end
then change view to:
<%= image_tag(product.image_url) if product.image_url.present? %>
if products
table populated remote urls app using other carrierwave, option might better write rake task download , re-save them carrierwave. might like:
product.all.each |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') |http| request = net::http::get.new uri http.request request |response| file.open(temp_location, 'w') |file| response.read_body |chunk| file.write chunk end end end end product.productimage = file.open(temp_location) product.save! file.unlink(temp_location) end
Comments
Post a Comment