I am trying to figure it out how can I dis attach an image from my active record storage.
Basically what I did so far is:
Installed Active Storage.
Place has_one_attached :avatar on my model.
Then on my form I placed the f.file_field:
<%= image_tag(#contact.avatar) %>
<%= f.file_field :avatar %>
Now I have this:
Remove
Which is a remove button. When a user clicks on this it must disattach the file or avatar image I've selected and simply depends on gravatar instead after removal.
Here's how I display my avatar image on the frontend:
<%= image_tag contact.avatar.attached? ? contact.avatar : contact.gravatar, class: 'media-object', style: "width: 80px" %>
I was thinking on how to implement this feature? Anyone could help me?
You can use purge like this
contact.avatar.purge
Check the guid for more options
Related
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 %>
Is there a way to pass an id via checkbox?
Let's say we have Model Record which stores information. Any user who want to edit a record will need to propose it first for the Admin to approve. The problem is Record is linked to multiple Image model (with paperclip attachment) so each Record has_many images. The problem is that I am stuck with how to allow users to propose a deletion of existing photos. I imagine it would be great to store a list of proposed deletion, with image_id and proposal_id to store the deletion waiting for approval but I can't find out a way to program the checkbox.
The checkbox would need to pass in the image_id marked to delete. Is it possible and if so how can I do that? Thanks!
<% #record.images.each do |image| %>
<%= check_box_tag "image_ids[]", image.id %>
<%= image_tag image.url %>
<% end %>
I am just looking for some clarification with using paperclip for saving images. I am grabbing all my images stored in Flickr using the Flickraw Gem. I am grabbing the url for the image and saving that to my model,
Model
class Portfolio < ActiveRecord::Base
attr_accessible :taken, :title, :url, :url_large
end
then rendering using the image_tag helper..
Like so
<%= #portfolio.each do |p| %>
<%= image_tag(p.url_large, :size => "480x480") %>
<%= p.title %>
<% end %>
So this shows all my photos at 480 x 480.. However I understand that paperclip handles images better?
So i can install paperclip, add :avatar column to my Portfolio model (though ill prob call it photo) and its the next part i want to clarify.
Do i save the url to the image within the :avatar column and then use the paperclip helpers as normal? Im used to uploading physical images to my model using paperclip which generates a file name within that column (well thats from what I can see)
I save the attributes like so at the moment
flickr.photos.search(:user_id => FLICKR_USER_ID).each do |p|
info = flickr.photos.getInfo(:photo_id => p.id)
title = info.title
taken = info.dates.taken
square_url = FlickRaw.url_s(info)
original_url = FlickRaw.url_o(info)
Portfolio.where(title: title, url: square_url, taken: taken, url_large: original_url).first_or_create!
end
So where to save FlickRaw.url_o ?
Can anyone advise if im thinking about this correctly or do i have some things wrong?
Any help appreciated
You could check this other post save image from url by paperclip they explain how to insert a picture from an url.
But actually I think this will try to upload the image.
<%= 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