user595419
user595419

Reputation:

How to the use the image_tag with a remote URL?

I've got a rake task which uploads images I've cached from an API to my S3 bucket. In my view, I try to output the image but it just doesn't appear to work. What I want to do is cache the images onto my filesystem, send them to S3 and I want to use the location of the image from my S3 bucket rather than my filesystem. My code looks like below:

In my rails console, I do this just to check the image url:

1.9.3p125 :002 > a.image
 => http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629 
1.9.3p125 :003 > 

I use Paperclip in my app, is it supposed to add the url as "http:://"? Seems rather weird. The code in my index.html.erb looks like this:

<li>
  <%= movie.title %>
  <%= image_tag movie.image.url %>
</li>

But this results in the following html:

<li>
  Cowboy Bebop
  <img alt="1" src="/assets/http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629">
</li>

Why does it include the '/assets'/ before my URL?

I configured Paperclip to set up the image url for my European S3 Bucket following a tutorial. So in my environment.rb, I've got this:

#Signature correction for Paperclip and AWS
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"

And I've got an aws-signature.rb file in my initialisers directory with this code:

#Makes Paperclip use the correct URL for images
Paperclip.interpolates(:s3_eu_url) { |attachment, style|
  "#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}

Upvotes: 2

Views: 2302

Answers (1)

Anthony Alberto
Anthony Alberto

Reputation: 10405

There's a problem with the URL : http::// instead of http:// so image_tag doesn't know it's an absolute URL.

How do you generate these URLs? Gem or your own code?

Upvotes: 2

Related Questions