Only display a user-uploaded photo if it exists in rails - ruby-on-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?

Related

Show image paperclip after query

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

Paper clip - stored image path inside active admin

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

Changing styles in paperclip to adjust image size makes the app display' no file chosen' when uploading an image?

In my model pin.rb
has_attached_file :image, styles: { medium: "320x240>"}
In pin.html.erb
<td><%= image_tag pin.image(:medium) %></td>
When I upload an image it gives
No File Chosen
Paperclip::Errors::NotIdentifiedByImageMagickError
However when I remove "styles..." from pin.rb and '(:medium) from pin.html it works fine. I want the image size to be adjusted though.
What am I doing wrong?
Aren't you forgetting to call url on the attachment?
<%= image_tag pin.image.url(:medium) %>
Yes, this is a couple of years old and was never answered
I'm researching another Paperclip question today, but in this case I would say:
<%= image_tag #pin.image.url(:medium) %>
as that's the equivalent of what I have in mine.
Don't forget the instance variable in the _form.html.erb as well as the .url method to set the image into the size you want.
For any travelers to this page, as a next step you might want to also look at another post here on how to delete the image in the Edit view. Rails file upload (paperclip) on edit
Cheers

wicked_pdf image rendering

how do I get images of a product to show in pdf?
I have this code in view file
<div id="img-slide">
<% for asset in #car.assets %>
<%= image_tag(asset.asset.url(:medium)) %>
<% end %>
</div>
and it shows all images for this car.
but if I use the same code in show.pdf.erb then instead of images I got only question marks.. like the image missing thing.
So, is there a way to get them on paper? Thanks.
p.s. there is what console is showing
***************WICKED***************
Asset Load (0.2ms) SELECT `assets`.* FROM `assets` WHERE (`assets`.car_id = 29)
Carmodel Load (0.2ms) SELECT `carmodels`.* FROM `carmodels` WHERE `carmodels`.`id` = 28 LIMIT 1
Rendered cars/show.pdf.erb (255.2ms)
"***************/usr/bin/wkhtmltopdf -q - - ***************"
update
<%= pdf_image_tag('/public/system/assets/163/medium/2011_lincoln_navigator_l_angularfront.jpg', :style=>"margin:0px;padding:0px", :width=>"300", :height=>"240")%>
this code shows how the link should look like in html, with this code I can render one photo of the car, but it will be the same for all cars, so I didn't do much.
the 163 number is the id of assets that is assigned to car, here I keep one image with more sizes(thumb, medium, large..) and I got 5 maps with different numbers for one car. So I have lots of maps with numberes like this as I have at least 5 photos for each car. each car have 5 assets. In show.html I can see them, but not in pdf. I did put this in application helper:
def pdf_image_tag(image, options = {})
options[:src] = File.expand_path(RAILS_ROOT) + '' + image
tag(:img, options)
end
but this is only for images that you have on your server and will be the same for all cars, how can I get at least one image of each car to show in pdf? Pleaseeeee. help!!!
Version 0.7.9 is
<%= wicked_pdf_image_tag 'myfile.jpg' %>
It basically returns the absolute path on the file system:
file:///path/to/image/myfile.jpg
Also see: https://github.com/mileszs/wicked_pdf
Instead of using wicked_pdf_image_tag helper it's possible to use image_tag and image_url rails helpers together to get absolute path to image (this is what required for wicked_pdf gem):
image_tag image_url('dir/image.png')
here dir/image.png is image path relative to standard location in rails app (app/assets/images for images).
ok, so I found the answer
the <%= image_tag(asset.asset.url(:medium)) %> code generates the url to the image that in html will look like <img alt="2012_bmw_7_series_gearshift" src="/system/assets/174/original/2012_bmw_7_series_gearshift.jpg?1318267462"> so my images starts in system map and all I had to do is to write a method in application_helper.rb like this:
module ApplicationHelper
def pdf_image_tag(image, options = {})
options[:src] = File.expand_path(RAILS_ROOT) + '/public' + image
tag(:img, options)
end
end
this way wiked_pdf will know that image tag called pdf_image_tag will start from system folder in public and the final code for the pdf.html.erb will be:
<% for asset in #car.assets %>
<%= pdf_image_tag(asset.asset.url(:medium), :style=>"margin:0px;padding:0px", :width=>"250", :height=>"200")%>
<% end %>
was easy, but still took me a few days to figure it out. Nice day.
The only thing that worked for me is to use this:
<%= image_tag wicked_pdf_asset_base64('logo') %>
While the answer of #tap349 worked a bit for me, I had to do something completely different for images already stored in the project.
So, for images uploaded by the users, I use the mini_magick gem and in my views I have (I use slime syntax):
= image_tag image_url(picture.url(:medium))
For images already stored in the project I had to manually create the absolute path, by returning the image location and joining it with the request protocol and host like this (I use Webpacker and my images are in app/javascript/images):
= wicked_pdf_image_tag URI.join(request.base_url, asset_pack_path('media/images/logo.png'))
In another post I saw this command that helped me to see if the path existed or not, you can try it in the ruby console:
Webpacker.manifest.send(:data).keys.grep /logo/

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