All the files uploaded by paper clip are beeing stored inside public/system/images/pictures/ and lots of 000/000/002 folders and etc.
I Want to use image_tag inside one active admin's view to print all the images related to one gallery.
I can get the image object, and of course the image name, but how i figure out the right stored path?
Just call url on the image object, like this:
<% #gallery.images.each do |image| %>
<%= image_tag image.url %>
<% end %>
Related
The App:
My application has many buses. Each bus has many photos of the interior and exterior. Some of the interior photos have an associated image that indicates the location on the bus where the photos were taken (like a "You are Here" marker on a map), which is assigned through :parent_id attribute that corresponds to the "parent" photos id.
The Goal:
Output the URL for the "child" image if a selected photo is a "parent," and keep blank if not.
The Problem:
The only way I know how to find any particular associated image brings back an array. Unfortunately for me, the photo.url method that comes with Paperclip can't work with an array.
This is closest to what I want, but, again, it brings back an array that I can't use to find the image URL.
def assigned_floorplan(where I pass in all the parent images as a params)
bus_images.all(conditions: { is_floorplan: true, parent_id: params.id })
end
Is this a dead end, or is there a way to pull out the id of the associated image from the array so that I can use the photo.url method? Or am I going about this the wrong way? I'm willing to approach this problem totally differently if you have any suggestions.
You have to call the photo.url method for each image separately. A nested loop would work:
<% #parent_images.each do |parent_image| %>
<%= parent_image.photo.url %>
<% parent_image.child_images.each do |child_image| %>
<%= child_image.photo.url %>
<% end %>
<% end %>
I used paperclip to let users upload their avatars. Everything works fine.
I want to show a default image if the user didn't upload an avatar. I used this code in my view:
<%=
if File.exist?(user.avatar.url)
image_tag user.avatar.url(:large)
else
image_tag "default-avatar.png"
end
%>
but it doesn't show the default image.
I put the default-avatar.png in app/assets/images/.
What am I doing wrong?
EDIT
I followed the instructions oldergod mentioned in comments, but still the uploaded avatars don't show up.
I finally put it to work, using user.avatar? like this:
<%=
if user.avatar?
image_tag user.avatar.url(:large)
else
image_tag "default-avatar.png"
end
%>
I think the problem is that File.exist? needs a path, not a url or something.
I'm pretty sure that there should be an easy way of doing this. I already tried out to override the models and discovered that the imagines seem to get saved in the database.
All i want it to be able to show all the oploaded images in the application view, so that they can be displayed on every page.
Currently I am new to Rails, I would be thankful for an easy guide or at least some hints.
They're saved as Image model instances and you can use the image_fu view helper to render them in whatever size you need. So in your view just do something along the lines of
<% Image.all.each do |image| %>
<%= image_fu image, "100x100", :id => dom_id(image) %>
<% end %>
<%= image_tag message.photo.url(:small) %>
I have paperclip installed and the above code displays an image that the user uploads which has been resized to :small (in this case 40x40px. How can I get my page to display this image only if it exists? Currently if the message includes a photo that photo is displayed but all other messages show broken image links. Thanks
Paperclip adds the name of the attachment suffixed with a "?" to the attached model as a helper method to allow you to see whether or not there is an attachment. In your case, the helper method would be photo? on the message class. You could use it with the tertiary operator in this manner:
<%= message.photo? ? image_tag message.photo.url(:small) : "" %>
Or, if you'd like to show a default no-image image when there is no image...
<%= image_tag message.photo? ? message.photo.url(:small) : url_to_no-image_image %>
It's been a while since I've used rails, but wouldn't it suffice to just put in an if statement?
I'm using Rails 3 paperclip and allow users to upload attachments to the attachment model.
If the file is an image, the app generates image previews. If the file is not, it only uploads the file (no image previews).
Now I would like to display a list of all the attachments in the DB. So I use attachment.attachment(:large) and that works fine for image attachments, but errors (obviously) for non-image attachments.
What's a good way to check if it's an image attachment or not? If not, I'd like to display a standard static image. Any suggestions? thanks
This is what I did in my view:
<% if !(#attachment.attachment.content_type =~ /^image/).nil? %>
<%= image_tag #attachment.attachment.url(:small) %>
<%end%>
This assumes that your model is attachment, and my file, I so called attachment.
So you could do something like:
<% if !(#attachment.attachment.content_type =~ /^image/).nil? %>
<%= image_tag #attachment.attachment.url(:small) %>
<%else%>
<%= image_tag "/path/to/image/default.png" %>
<%end%>
Check attachment.attachment.attachment_content_type
For example, it might be: "image/jpeg"
You can create a migration that adds a attachment_content_type field of type string to your attachment table. When you create an attachment, paperclip stores the type of the file in that field. You can then check if the file type is something like "image/jpeg".
Maybe you could use default_url option? That would be shown if the real thumbnail doesn't exist.
http://www.suffix.be/blog/default-image-paperclp