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

Display documents uploaded with paperclip rails

我正在为我的 post 使用回形针,以允许用户上传带有他们 post 的图像。

然而,当我做同样的概念但供用户上传文档(doc、pdf 等) 我收到错误:

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

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

这是我的 Post 模型:

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" %>

两个return同样的错误。

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

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)

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

有: validates_attachment_type