how to get the image from the url without downloading it but store it in a variable in ruby

544 Views Asked by At

How to fetch the image from its URL and store it in a variable

As im having a URL of a image, i need to fetch that image and then store it in variable and display the image in the variable using the rails image tag, so how to approach this..

Thanks in advance

the method i tryed

require 'open-uri'

imagevar =  open(url).read

img_tag(imgvar) 

which resulted in following error

ActionView::Template::Error: string contains null byte

so what will the correct solution to approach this

1

There are 1 best solutions below

0
Kedarnag Mukanahallipatna On

To assign the URL to a variable in your .erb, do the following.

<% image_url = "<image_url>" %>

To render the image from the URL you can use image_tag

<%= image_tag image_url %>