2016-09-09 9 views
0

pdfはそこにあります。を表示するPDF

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)} 

私が説明と名前のsample.pdfがファイルのアップロードしたインスタンスを検索することができるように、私もgem 'friendly_id'を使用しています。

そのページ(http://localhost:3000/documents/sample.pdf)を入力するけれども、私が取得:
Missing template /document with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}
は、私はそれが:formats=>[:pdf]とは何かを持って推測しています。

私は<%= link_to "document", asset_path(document.file.url) %>を作成するときに、私はこのリンクを取得:
http://localhost:3000/documents/sample.pdf

マイルートファイル:私はより多くのようになりたい
http://localhost:3000/Users/user/Desktop/testsite/documents/sample.pdf?1473447505

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

私は私はこれについて正しいことをやっているのかどうかは分かりません。

どのような提案も素晴らしいでしょう。

+0

あなたのルートファイルを共有できますか? –

+0

@Udaykumardas私はあなたの質問にそれの要点を入れました。しかし、私は基本的に 'Document'というクラスにそのファイルを含めています。 'Document'は':name、:description、:file、 ':' slug'を持ちます。 :ファイルは 'assets/documents /'の場所にあるフォルダにアップロードされます。どこにpdfにアクセスしたいのですか?www.testsite.com/documents/sample.pdf – Corey

答えて

1
あなたのリンクは、PDFファイル

をダウンロードするように求められた場合にその後documents_controller.rb

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

でこの質問をチェックしてください

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

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

を変更

Forcing the inline rendering of a PDF document in Rails

+0

これは完全に動作します!ドキュメントコントローラーの部分は、本当に苦労していた場所でした。 – Corey