1

Trying to show a pdf by linking to a URL of www.testsite.com/documents/sample.pdf

I'm using paperclip to upload the pdf, and can see that the pdf is there.

    has_attached_file :file,
    :url => ":rails_root/documents/:filename",
    :path => ":rails_root/public/assets/documents/:id/:filename"

    validates_attachment :file, :content_type => {:content_type => %w(image/jpeg image/jpg image/png application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)}

I'm also using the gem 'friendly_id' so that I can search my uploaded instance of the sample.pdf file with the description and name.

When entering that page(http://localhost:3000/documents/sample.pdf) though, I get:
Missing template /document with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}
I'm guessing it has something to do with the :formats=>[:pdf].

When I create a <%= link_to "document", asset_path(document.file.url) %>, I get this link:
http://localhost:3000/Users/user/Desktop/testsite/documents/sample.pdf?1473447505
Which I would like to be more like:
http://localhost:3000/documents/sample.pdf

My Routes file:

Rails.application.routes.draw do
  resources :documents
  root 'main_pages#home'
  get 'home' => 'main_pages#home'
  get '*path' => redirect('/')
end

I'm not sure if I'm going about this the right way at all.

Any suggestions would be great.

Corey
  • 835
  • 1
  • 9
  • 32
  • can u share your route file. – Uday kumar das Sep 09 '16 at 19:53
  • @Udaykumardas I've put the gist of it in the question for you. But I'm basically just including that file in a class called `Document`. `Document` has a `:name, :description, :file,` and `:slug`. The :file is uploaded to a folder in the `assets/documents/` location. Which is where I would like to access the pdf, but using a url like: www.testsite.com/documents/sample.pdf – Corey Sep 09 '16 at 20:08

1 Answers1

2

Change your link

<%= link_to "document", asset_path(document.file.url) %>

To

<%= link_to "document", document_path(document) %>

Then in documents_controller.rb

def show
  send_file(document.file.url, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")
end

Check this question in case it asks to download the PDF file

Forcing the inline rendering of a PDF document in Rails

Community
  • 1
  • 1
Erick Eduardo Garcia
  • 1,147
  • 13
  • 17
  • This works perfectly! The documents controller part was where I was really struggling, thanks! – Corey Sep 09 '16 at 22:10