Displaying PDFs in Ruby on Rails - 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.

Related

Rails 5: image and loop of images are not showing in email

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.

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)

Rails display related posts by tags with images from carrierwave

postI'm trying to retrieve related posts by tags by showing the attached image per related post, uploaded by using the carrierwave gem
Tags were written from scratch but similar to acts-as-taggable-on gem.
Below solution picked from Rails count article per tag and related article by tag displays the related posts by title.
<% #posts.each do |post| %>
<% post.tags.each do |tag| %>
RELATED POSTS:
<% tag.posts.each do |related_post| %>
<%= link_to related_post.title + " , ", post %>
<% end %>
<% end %>
<% end %>
Now I want related posts to be displayed by image instead of title per above. Code displaying image for post is
<%= image_tag(post.cover.url(:thumb)) if post.cover? %>
How can I fit this in the RELATED POSTS above?
I tried
<%= image_tag(related_post.cover.url(:thumb)) if post.cover? %>
but this throws:
NameError: undefined local variable or method 'related_post' for #<#:0x007fb67db38fb8>
Obviously related_post is not recorded in the carrierwave uploader model since it is different from the tags model. Any help please?

Displaying an image in erb that's hosted elsewhere online

I'm having a little trouble trying to display an image that is hosted on another site. Usually I would have the image in my assets pipeline, but on this project I am using an API to grab the image URL that's hosted on another site. I'm trying it this way below, but I feel like it's still pointing to my assets folder. How can I set it to display this image if it meets a conditional? Thanks!
...
<% if article.image_url %>
<%= image_url("<%= article.image_url %>") %><p>
<% end %>
...
Try <%= image_tag(article.image_url) if article.image_url %>
Hope this helps!
I believe you'd need to interpolate article.image_url like so:
<% if article.image_url %>
<%= image_tag article.image_url %><p>
<% end %>

Including the contents of a javascript file

If there a way to include the content of a javascript file into a Rails view?
I know about Ruby's File.read, but I'm searching for some helper already done.
<%= javascript_tag do %>
<%= render :file => '/path/to/rails/app/path/to/javascript.js' %>
<% end %>
So you can include any javascript code located on your machine. But to be honest there's more appropriate way to do that kind of things.

Resources