saeta
saeta

Reputation: 63

How to call an external website image with image_tag in a .html.erb?

How can I call an image of some url external website using image_tag in a rails html view? Please, I'm looking for an alternative using image_tag not the regular html or css

Upvotes: 2

Views: 4477

Answers (2)

Cronwel
Cronwel

Reputation: 113

Say you have an SVG file that is the image and it points to your Github or Linkedin profile, you could do something like this:

Setup:

  1. In app/images/, you have some image file. For example, linkedin.svg.

Procedure

  1. In your html.erb file, add:

<%= link_to image_tag("linkedin.svg"), "http://www.linkedin.com/in/myprofile",  :target => "_blank"%>
  1. Wait!!!!! READ CAREFULLY Pay close attention to the space between image_tag and the parentheses(the method and the argument). Some examples that you'll find online show a space between the image_tag and the argument. Keep them together.
  2. Check the commas.
  3. Last, just to be safe, make sure your link includes "http://www....."

Upvotes: 0

aspencer8111
aspencer8111

Reputation: 786

EDIT Turns out OP wanted to do this b/c Heroku wasn't pulling in images from the assets folder. It is an issue that is easily resolved by reading/following the instructions here: Rails 4 images not loading on heroku .

Leaving the original answer here as it more correctly answers the original question.

image_tag works with full urls. So you could just give it the URL to the site you want to pull from. i.e:

<%= image_tag 'https://placekitten.com/800/400' %>

I'm sure you are already aware of this, but in 99.999% of situations, hot-linking images from other sites is a very bad idea.

Upvotes: 1

Related Questions