Show image paperclip after query - ruby-on-rails

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 %>

Related

Disattached an image using Rails Active Storage

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

How to upload image using paperclip without model in Rails 3?

I want to implement Papereclip in Rails 3.2 without a model.
I am very new to RoR and Paperclip.
I followed through THIS tutorial to use paperclip. It is running successfully.
But in this tutorial they have shown use of the model, I dont want to use the model or dont want to modify the existing model.
Rails is object oriented.
# Article model
attr_accessible :image
# Paperclip attachment code and whatever rules and conditions you want.
The image attribute is actually a column from your database and an attribute accessible from your Article model. That image attribute will need to be a string type (varchar) to store the url where the image is saved.
For example if your article has an image attribute, in your view file Article#Show you can display your image through the article object with the image attribute as <%= image_tag #article.image.url %>.
If you want to have many images in a same article then you will want to create 2 models (Article and Image) and have an association where
# Article model
has_many :images
accepts_nested_attributes_for :images
# Image model
attr_accessible :image_url
belongs_to :article
# paperclip attachments...
With this association you can then fetch and display your images by doing
<% #article.images.each do |image| %>
<%= image_tag image.image_url.url %>
<% end %>
Technically you don't need to store the url of your image in the database, you can store it in variable, but then it will only be valid during the session and will disappear afterwards. If you want to view your image at a later date, then yes you need to store it in a database.
For not using a seperate model you can check Paperclip Docs. What is basically done here is that the image is stored as an atrribute of an existing model.
About not using a model at all, how do you want to associate the image object to some existing record ? Wont that be required in your project ?

Saving Images using Paperclip

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.

Only display a user-uploaded photo if it exists in rails

<%= 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?

Rails PaperClip Attachments, knowing if there's a image thumbnail?

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

Resources