【发布时间】:2021-03-27 11:16:32
【问题描述】:
使用 Shrine 启动了一个全新的 Rails 6 应用程序,一切都是全新的,跟随入门指南。成功上传文件,但在显示页面上,img 标签只是一个损坏的图像图标,但如果我在新选项卡中打开它,它会显示。控制台中没有显示错误。
/config/initializers/shrine.rb
require "shrine"
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads") # permanent
}
Shrine.plugin :activerecord # loads Active Record integration
Shrine.plugin :cached_attachment_data # enables retaining cached file across form redisplays
Shrine.plugin :restore_cached_data # extracts metadata for assigned cached files
app/uploaders/image_uploader.rb
class ImageUploader < Shrine
# plugins and uploading logic
end
app/models/content.rb
class Content < ApplicationRecord
include ImageUploader::Attachment(:image) # adds an `image` virtual attribute
end
views/contents/show.html.erb
<p>
<strong>Image:</strong>
<%= image_tag @content.image_url %>
</p>
页面来源
<img src="/uploads/efe42a8e0d19d3ffcabd26bcddd71473.mp4">
【问题讨论】: