【问题标题】:Uploading Documents (DOC/DOCX) to Rails with Paperclip使用 Paperclip 将文档 (DOC/DOCX) 上传到 Rails
【发布时间】:2015-04-30 09:48:42
【问题描述】:

我需要允许我的用户将 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

【问题讨论】:

  • 显示您的视图和路线.rb
  • @Yule 为我的问题添加了视图和路线。

标签: ruby-on-rails paperclip


【解决方案1】:

因为您传递了@document,它是一个文档,所以rails 会查找documents_path 的路径,该路径不存在,因为您已将它嵌套在酒店内。您还需要指定酒店:

<%= simple_form_for([@hotel, @document], multipart: true) do |f| %>
  ...

记得定义@hotel

【讨论】:

  • 谢谢,但这不是问题。该文档绕过了控制器中的“if @document.save”操作,并落入了 else 部分。有任何想法吗? PDF 和 IMG/GIF 可以完美运行。
  • 如果没有看到控制器,我不可能发表评论。
【解决方案2】:

可能有点晚了,但在遇到同样的问题后,我找到了一个适合我的解决方案。只需将以下内容放入名为(或创建)config/initializers/mime_types.rb

的文档中
Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx

并且 docx 上传应该使用引用的 content_type。

现在如果有人有兴趣像我一样只上传 pdfdocdocx,只需将以下内容放入您的模型中 p>

validates_attachment_content_type :your_column_name, :content_type => ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-22
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2013-09-04
    • 2014-10-16
    相关资源
    最近更新 更多