显示使用回形针导轨上传的文档

Posted

技术标签:

【中文标题】显示使用回形针导轨上传的文档【英文标题】:Display documents uploaded with paperclip rails 【发布时间】:2020-05-17 16:04:07 【问题描述】:

我在帖子中使用回形针,以允许用户在帖子中上传图片。

但是,当我做同样的概念但让用户上传文档(doc、pdf 等)时 我得到了错误:

No route matches [GET] "/documents/original/missing.png"

似乎帖子已上传,但文档未存储在数据库中。

这是我的帖子模型:

class Post < ApplicationRecord
  belongs_to :category
  belongs_to :user
  validates :user_id, presence: true
  validates :category, presence: true

  has_attached_file :image, styles:  medium: "300x300>", thumb: "100x100>" 
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/

  has_attached_file :document
  validates_attachment_content_type :document, content_type:  content_type: %w(application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document) 
end

这是我的迁移:

class AddAttachmentDocumentToPosts < ActiveRecord::Migration[6.0]
  def self.up
    change_table :posts do |t|
      t.attachment :document
    end
  end

  def self.down
    remove_attachment :posts, :document
  end
end

我尝试通过以下方式显示文档:

<iframe src="<%= @post.document.url %>"></iframe>
AND
<%= link_to "My document", @post.document.url, target: "_blank" %>

两者都返回相同的错误。

我认为这可能是因为我没有在我的参数中允许它。但是当我这样做时,我什至不能再上传文件了。我在我的日志中得到了这个:

Started POST "/posts" for ::1 at 2020-02-01 02:01:26 +0100
Processing by PostsController#create as html
  Parameters: "authenticity_token"=>"g/XEFJGg3W9WwjBqasme4KnGwqTeL4HyZJztI0AvJC5D4nuZx7nUVBVMM5jSkCORMhO1ItJo3X/RvuuhJqpwjQ==", "post"=>"title"=>"d", "body"=>"d", "category_id"=>"1", "document"=>#<ActionDispatch::Http::UploadedFile:0x00007fb0369bad50 @tempfile=#<Tempfile:/var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/RackMultipart20200201-96604-1sh7xm.pdf>, @original_filename="test.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"post[document]\"; filename=\"test.pdf\"\r\nContent-Type: application/pdf\r\n">, "commit"=>"Create Post"
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/posts_controller.rb:36:in `create'
[paperclip] Trying to link /var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/RackMultipart20200201-96604-1sh7xm.pdf to /var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/098f6bcd4621d373cade4e832627b4f620200201-96604-8ry78w.pdf
[paperclip] Trying to link /var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/098f6bcd4621d373cade4e832627b4f620200201-96604-8ry78w.pdf to /var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/098f6bcd4621d373cade4e832627b4f620200201-96604-1h5w1ey.pdf
Command :: file -b --mime '/var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/098f6bcd4621d373cade4e832627b4f620200201-96604-1h5w1ey.pdf'
   (0.1ms)  begin transaction
  ↳ app/controllers/posts_controller.rb:39:in `block in create'
  Category Load (0.3ms)  SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/posts_controller.rb:39:in `block in create'
[paperclip] Trying to link /var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/098f6bcd4621d373cade4e832627b4f620200201-96604-8ry78w.pdf to /var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/098f6bcd4621d373cade4e832627b4f620200201-96604-vzv89v.pdf
Command :: file -b --mime '/var/folders/xc/f72yt5g91mq_4trwxgnc72n40000gq/T/098f6bcd4621d373cade4e832627b4f620200201-96604-vzv89v.pdf'
   (0.1ms)  rollback transaction
  ↳ app/controllers/posts_controller.rb:39:in `block in create'
  Rendering posts/new.html.erb within layouts/application
  Rendered posts/_navbar.html.erb (Duration: 0.5ms | Allocations: 734)
  Category Load (0.1ms)  SELECT "categories".* FROM "categories"
  ↳ app/views/posts/_form.html.erb:9:in `map'
  Rendered posts/_form.html.erb (Duration: 1.9ms | Allocations: 1394)
  Rendered posts/new.html.erb within layouts/application (Duration: 2.7ms | Allocations: 2263)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 76ms (Views: 39.3ms | ActiveRecord: 0.7ms | Allocations: 16989)

【问题讨论】:

【参考方案1】:

问题出在我的帖子模型中。 我只需要更换: validates_attachment_content_type

与: validates_attachment_type

【讨论】:

以上是关于显示使用回形针导轨上传的文档的主要内容,如果未能解决你的问题,请参考以下文章

带导轨的图像/照片库(网格视图)?

使用回形针 Rails 3 上传的 id 显示来自另一个模型的图像

确定具有特定风格的图像存在和不存在 - 回形针导轨

Rails 回形针,多种不同类型(PDF、图像、文档...)

如何使用Paperclip在Rails 4中上传多个图像

回形针上传的文件被删除了?