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
Related
Rails: 6.1
Following the basic steps in the documentation of ActiveStorage I have added a avatar field for my User model and upload seems to be successful. But when I want to display #user.avatar I get a url for the image but the url seems to be 404. The image is not displayed.
Here is my image tag:
<img src="<%= image_path(url_for(#user.avatar)) %>" alt="Avatar for <%= #user.username %>">
<%= image_tag #user.avatar, class: 'user-image', alt: "Avatar for #{#user.username}" %>
Both of these are failing. Rendered HTML is here:
<img src="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--569f9d466f5dd0d4917db040f0f87d948c0af667/paddle.png?locale=en" alt="Avatar for Someone">
Looking at the path, paddle.png is the image I uploaded and I assume upload is okay. But why doesn't it display?
Try this:
<%= image_tag #user.avatar.to_s, class: "user-image" %>
Okay, finally I found what was wrong. First thing ActiveStorage is not compatible with UUID models. What's more for a given 12hefhs-342jfsoeif-senfs-senfks UUID it takes the first consecutive digits and uses it as ID. Which is in my opinion a big issue. We don't get any error message like we do for other rails models when we try to save UUID in bigint (default id) field. To overcome this problem I had to add these migrations in this gist
Then the other problem was that I have a regex to redirect all requests to locale version of that same path. Like this:
get "/*path",
to: redirect("/#{I18n.default_locale}/%{path}", status: 302),
constraints: { path: /(?!(#{I18n.available_locales.join("|")})\/).*/ },
format: false
and this was redirecting image urls as well...
Removing this solves the problem. But introduces another one... How to redirect...
I have a column called resolution_image type boolean in the paperclip images table. I want to show the image where the field resolution_image is true, how do i show this image?
<%= image_tag #model.picture.url if #model.resolution_image %>
Need some more details on your specific naming convention, but let's assume the following:
You have a model with a paperclip attachment named model_with_image_attachment
That model has an attachment which you've named attachment_name
This would display the image when the resolution_image attribute is true:
<%= image_tag model_with_image_attachment.attachment_name.url if model_with_image_attachment.resolution_image %>
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.
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 %>
<%= 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?