reb_292_
reb_292_

Reputation: 11

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.

Upvotes: 0

Views: 172

Answers (1)

reb_292_
reb_292_

Reputation: 11

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.

Upvotes: 1

Related Questions