Rails 5: image and loop of images are not showing in email - ruby-on-rails

I have this in the mailer:
attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png")
And in the email template I have this:
<%= image_tag attachments['logo.png'].url %>
It works just fine and the logo does appear in the email.
Now this is the weird part, I have another image that is not in the asset pipeline but stored in the database and also a loop of other images stored in the database. And none of them appear in the email. The email goes through without any errors. Any idea what might be wrong and how I can debug this?
<%= image_tag #account.image.thumb.url %>
<% #attachments.each do |attachment| %>
<%= image_tag attachment.images.thumb.url %>
<% end %>
I'm using the gem carrierwave to attach image and I uploading this version:
version :thumb do
process resize_to_fill: [1024, 768]
end

There are two options here: either your images, stored in the databases, also have a public-facing url, and then you can use those. But most likely they do not, and then you have to add the images as attachments to your email as well, and then use the attachments when building the email.
So in your mailer write something like:
#attachments.each do |attachment|
attachments.inline[attachment.image.original_filename] = attachment.image.thumb.read
end
and then in your mail view, you can just iterate over all #attachments again, and use the correct attachments.inline[...], something like
<% #attachments.each do |attachment| %>
<%= image_tag attachments.inline[attachment.image.original_filename].url %>
<% end %>
Note: I am not entirely sure of the correct syntax here (you use images but I presume it should be singular? also you should check what is the best unique way for your image, maybe the original_filename is not ideal, you could also just use the id of the attachment.

Related

Displaying PDFs in Ruby on Rails

I'm new to Ruby on Rails. I successfully implemented Active Storage to store files on my site. Now I'm trying to figure out how to display my attachments. I've mainly been following this documentation: (https://edgeguides.rubyonrails.org/active_storage_overview.html#displaying-images-videos-and-pdfs)
This is my code:
<% if #location.document.representable? %>
<%=image_tag #location.document.representation(resize_to_limit: [100, 100]) %>
<%="TRUE"%>
<% else %>
<%= link_to rails_blob_path(#location.document, disposition: "attachment") do %>
<%= image_tag "placeholder.png", alt: "Download document" %>
<%#"BROKEN"%>
<%="FALSE"%>
<% end %>
<% end %>
Currently, my problem is that the code generates the generic image not found image. I think the problem is '#location.document.representation' because most of the examples I've seen follow a variable.representation format (for example, document.represent). Most examples I found iterate through multiple attachments, but I only have document attached. The puzzling aspect is that it prints TRUE, meaning #location.document is representable. As I am a beginner, I know there is something I'm missing.
Update: I figured out the problem. If you scroll to the top of the documentation, you'll see you need to include this gem:
gem "image_processing", ">= 1.2"
The code itself was fine, it just needed to actually process the image to display.

undefined method 'to_model' when linking thumbnail to original image

I'm creating an album that I'm planning to display with Masonry and ImgZoom so that the visitors can have a bigger image when they click on it.
According to ImgZoom, to make the zoom work, you need to do the following:
<a href="path/to/real/image.png">
<img src="path/to/image's/thumbnail.png class="thumbnail" />
</a>
So I generated an uploader, with the following inside it:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
'portfolio/photos'
end
version :thumb do
process :resize_to_fit => [220, nil]
end
end
Everything works perfectly, I can call both the versions without trouble, but when I try to follow ImgZoom's instructions by doing the following:
<%= #portfolio.photos.each do |p| %>
#This is a nested form inside the portfolio form, so I need to do this to get my images
<%= link_to image_tag p.image.thumb.url, p.image %>
or:
<%= link_to p.image do %>
<%= image_tag p.image.thumb.url, :class => 'thumbnail' %>
<% end %>
I'm getting the following error: undefined method 'to_model' for #<ImageUploader:0x0000000c35f4d8>
I found a similar subject on stack overflow but the asker wasn't clear and was invited to ask an other question on the forum, which I couldn't find.
I can individually reach 'p.image' and 'p.image.thumb.url', but I can't make a link from one to another, which would be perfectly doable with simple html.
What am I doing wrong?
Thank you in advance
First, to create the class "thumbnail" in the link, you need to declare it properly. I edited the link:
<%= link_to p.image.url do %>
<%= image_tag p.image.url, class: "thumbnail" %>
<% end %>
Second, you need to check if you created an appropriate route for viewing the image. This can be either done by linking to a static assets properly (as your image is not under "public") or via a template view.
If the files where stored under "public", your way of linking should work just fine.
Check out how image_path works in the docs: image_path (and more)

How to create a link to a non-image file (like a word or excel file) using Dragonfly and Rails?

I've got a rails app with the ability to upload multiple types of files and now i'm trying to create a page to show the data to the user.
I have this snippet in my view:
<% #case.documents.each do |doc| %>
<% if doc.is_image? %>
<%= image_tag doc.document.url %>
<% else %>
<%= link_to doc.filename, doc.document.url, target: '_blank' %>
<% end %>
<% end %>
which generates a link like this for a file that is not an image.
<a target="_blank" href="/media/W1siZiIsIjIwMTQvMDcvMjgvOHM3d21ubDBtc19maWxlIl1d?sha=0813902f">myFile.xlsx</a>
The problem is that since the href doesn't link directly to a filename with an explicit extension, the browser is not sure how to handle the file (at least I think that's why it doesn't know how to handle the file).
How can I make a link to these files which have been uploaded so the browser knows how to handle files like excel, word, pdf, etc.
the easiest way is to add a document_name column to your Document model - then the urls will have the file extension.
(although having said that, the Content-Type should be correctly set anyway so not sure why the browser is having trouble)

Strings not displayed right

So, I have this which displays emails to a user.
OLD CODE FOR REFERENCE:
<%= for email in #emails
# print the name
eml = email
eml
puts "<br>"
end
%>
FIXED, WORKING, STABLE CODE:
<% for email in #emails %>
<%= email %>
<br>
<% end %>
<%= puts #emails.inspect %>
As you can see, it was a problem of multiple line tag. Bazar that It would cause this problem, but not at all that it would cause A problem.
OLD:
And it is working great. One thing. So, EML is a ruby string with HIDDEN#HIDDEN.HIDDEN, but when it goes to display I get this on the rendered page: ["HIDDEN#HIDDEN.HIDDEN"], so why is it doing that? Inspected it, it isn't a hash. Just a string. What is happening here?
This syntax does not look quite right. If this is being rendered in a view using ERB, you probably want code that looks more like this:
<% #emails.each do |email| %>
<%= email %><br />
<% end %>
The way you have that written is very C#-looking. In Ruby it is more common to use the methods attached to the object. Enumerable objects like arrays could be iterated through using the each method and a special structure in Ruby called a block.
http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-each

Admin approve Paperclip images before display

Rails: How can I add admin approve functionality where user uploaded images have to be approved before they are displayed on their profiles?
How about add a column to the user's avatar that says admin_approved or something similar? Since you're using ActiveAdmin, it should pick up the column (unless you've specified otherwise). You then add a check before displaying the image:
<%= user_images.each do |image| %>
<% if image.admin_approved %>
<%= image_tag image.url %>
<% end %>
<% end %>

Resources