[英]Show PDF in Ruby on Rails assets
嘗試通過鏈接到www.testsite.com/documents/sample.pdf的URL來顯示pdf
我正在使用回形針上傳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)}
我還使用了gem 'friendly_id'
以便可以使用說明和名稱搜索我上載的sample.pdf文件實例。
當進入該頁面( 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/Users/user/Desktop/testsite/documents/sample.pdf?1473447505
我想更像是:
http://localhost:3000/documents/sample.pdf
我的路線文件:
Rails.application.routes.draw do
resources :documents
root 'main_pages#home'
get 'home' => 'main_pages#home'
get '*path' => redirect('/')
end
我不確定是否要以正確的方式進行操作。
任何建議都很好。
改變你的鏈接
<%= link_to "document", asset_path(document.file.url) %>
至
<%= link_to "document", document_path(document) %>
然后在documents_controller.rb中
def show
send_file(document.file.url, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")
end
如果要求下載PDF文件,請檢查此問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.