![](/img/trans.png)
[英]How to upload .docx .xls .xlsx documents using AngularJS and Rails Paperclip?
[英]Uploading Documents (DOC/DOCX) to Rails with Paperclip
我需要允許我的用戶將 Word 文檔(.doc 文件)上傳到我的應用程序。 圖像和 PDF 的工作正常,但是當我嘗試上傳文檔時,我得到:
undefined method `documents_path'
這是我的 Document.rb 文件中的代碼。
validates :title, presence: true
validates :file,
attachment_content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif", "application/pdf", 'application/msword','applicationvnd.ms-word','applicaiton/vnd.openxmlformats-officedocument.wordprocessingm1.document', 'text/plain']},
attachment_size: { less_than: 5.megabytes }
has_attached_file :file, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '350x350>',
pdf_preview: '600x600>'}, processors: [:thumbnail]
這是我的觀點:
<%= simple_form_for(@document, multipart: true) do |f| %>
<%= f.input :title, :label => "Name of Document" %>
<%= f.input :hotel_id %>
<%= f.file_field :file, :label => "Upload Document" %>
<%= f.submit 'Submit', :class => "btn btn-primary" %>
<% end %>
還有我的路線:
resources :hotels do
resources :advertisers
resources :documents
end
因為您傳遞了@document,它是一個Document,所以rails 會查找documents_path 的路徑,該路徑不存在,因為您將它嵌套在hotels 中。 您還需要指定酒店:
<%= simple_form_for([@hotel, @document], multipart: true) do |f| %>
...
記得定義@hotel
可能有點晚了,但在遇到同樣的問題后,我找到了一個對我有用的解決方案。 只需將以下內容放在名為(或創建)的文檔中config/initializers/mime_types.rb
Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx
並且 docx 上傳應該與引用的 content_type 一起使用。
現在,如果有人有興趣像我一樣只上傳pdf 、 doc和docx ,只需將以下內容放入您的模型中
validates_attachment_content_type :your_column_name, :content_type => ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.