Показать документы, загруженные с помощью скрепок - PullRequest
0 голосов
/ 01 февраля 2020

Я использую скрепку для своих сообщений, чтобы позволить пользователям загружать изображение с их сообщением.

Однако, когда я делаю ту же концепцию, но для пользователей загружаю документы (делайте c, pdf et c) Я получаю сообщение об ошибке:

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 Ответ

0 голосов
/ 01 февраля 2020

проблема была в моем посте модели. я просто должен был заменить: validates_attachment_content_type

на: validates_attachment_type

...