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



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

但是,当我做同样的概念但让用户上传文档(文档、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"rnContent-Type: application/pdfrn">}, "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)

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

跟: validates_attachment_type

最新更新