getting url from paperclip (RoR)
for some reason, I try to get the url of an image in various ways. I am using paperclip. None of them work.
I've tried
img.image.url
img.image.url(:thumbnails)
img.image.url(:display)
... when I try img.id, the id returns back just fine, and prints. the image also displays when I put image_tag before it. however, I am trying to preload images... and I need the exact url, and it just will not return : (.
Edits==============
This is what my js function looks like:
onLoadScroller: function() {
// preload images here
id = <%= @other_images[4].id %>;
alert(id);
var url = <%= @other_images[4].image.url %>;
alert(url);
img = new Image;
img.src = <%= @other_images[4].image.url %>;
alert(img.src);
},
I get id back just fine, but I don't see any alert pop up for url or img. I do see the correct url pop up from the console though. So this should be the right way of doing it. I'm not sure what could be breaking the javascript though?!
My model looks like this:
class Image < ActiveRecord::Base
has_attached_file :image, :styles => { :display => "500x500>",
:thumbnail => "95x95>"},
:default_style => :display
Update+++++++++++++
It seems like the img.src is turning out just correctly in the rendered code, but I am getting 开发者_高级运维"Uncaught ReferenceError: original is not defined" for some reason, preventing the alert from firing.
Alas, it was a matter of putting quotes around the url... thanks for your help...
img.src = "<%= @other_images[4].image.url %>";
If the image is correctly displayed when you put image_tag
before it, then your call is returning the exact URL (since it's good enough for the browser to download the image). I'm also using paperclip in my app and I'm getting the URLs in exactly the same way you gave.
I suggest you look for an error in you JS code, or wherever you're trying to preload the images.
精彩评论