首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Shrine on Rails 6创建的缩略图的URL出现问题

使用Shrine on Rails 6创建的缩略图的URL出现问题
EN

Stack Overflow用户
提问于 2021-11-20 21:41:41
回答 1查看 70关注 0票数 0

我已经在这个问题上挣扎了几天,希望有人能帮上忙。我可能遗漏了一些明显的东西!

我正在使用Rails的神社插件上传PDF文件,并为第一页生成缩略图。PDF将加载到指定的存储空间中,缩略图也将在同一位置创建和排序。但是,我无法在视图中显示缩略图。

初始化器:

代码语言:javascript
复制
require "shrine"
require "shrine/storage/file_system"
#require "shrine/storage/memory"

Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"),
    store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store")
}

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
Shrine.plugin :determine_mime_type
Shrine.plugin :validation_helpers
Shrine.plugin :validation
Shrine.plugin :derivatives
#Shrine.plugin :model, cache: false

上传者:

代码语言:javascript
复制
require "image_processing/mini_magick"

class FileUploader < Shrine
 include ImageProcessing::MiniMagick

 plugin :processing # allows hooking into promoting
 plugin :versions   # enable Shrine to handle a hash of files
 plugin :derivatives
 plugin :default_url
# plugin :delete_raw # delete processed files after uploading

 Attacher.validate do
  validate_max_size 5*1024*1024, message: "is too large (max is 5 MB)"
  validate_mime_type %w[application/pdf]
 end

 Attacher.derivatives do |original|
  magick = ImageProcessing::MiniMagick.source(original).loader(page:0).convert("jpeg")
  {
   thumb: magick.resize_to_limit!(200, 200) 
  }

 end

end

测试视图:

代码语言:javascript
复制
<p id="notice"><%= notice %></p>

<p>
  <strong>Number:</strong>
  <%= @issue.number %>
</p>

<p>
  <strong>Title:</strong>
  <%= @issue.title %>
</p>

<p>
  <strong>Data:</strong>
  <%= @issue.file_data %>
</p>

<p>
  <strong>Issue Preview</strong>
  <embed src="<%= @issue.file_url %>" width="80" height="160" />
</p>

<p>
  <strong>Issue Thumbnail:</strong>
  <%= image_tag @issue.file_url(:thumb) if @issue.file %>
</p>

<p>
  <strong>Issue:</strong>
  <%= image_tag @issue.file_url if @issue.file %>
</p>

<%= link_to 'Edit', edit_issue_path(@issue) %> |
<%= link_to 'Back', issues_path %>

此外,当我查看存储在file_data字段中的数据时,我没有看到为缩略图添加的信息。

文件数据:{"id":"fccd20a9323aa5b63fd912f4ca833ebb.pdf","storage":"store","metadata":{"filename":"pdf_sample.pdf","size":351987,“mime_type”:“应用程序/pdf”}}

EN

回答 1

Stack Overflow用户

发布于 2021-11-21 19:40:25

您需要在将附件升级到永久存储时启用自动创建:

代码语言:javascript
复制
Shrine.plugin :derivatives, create_on_promote: true

或者在附件中手动处理衍生工具:

代码语言:javascript
复制
record.file_derivatives!
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70050080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档