无法使用带有 ActiveStorage 的变体显示图像 image_processing gem

Unable to display image using variant with ActiveStorage image_processing gem

目前我正在开发 Rails 5.2 应用程序。当我尝试使用 variantshow.html.erb 模板中显示个人资料头像时,它不起作用

<%= image_tag @profile.avatar.variant(resize_to_fit: [100, 100]) %>

我得到以下内容

我看不到图片。

我在我的 Gemfile 中安装了 gem 'image_processing', '~> 1.2'

我也在 application.rb 添加了 config.active_storage.variant_processor = :vips

但是,在后端我仍然收到 500 错误:

Started GET "/rails/active_storage/representations/xxxx/steven.jpeg" for 127.0.0.1 at 2018-09-26 16:33:21 -0400
Processing by ActiveStorage::RepresentationsController#show as JPEG
  Parameters: {"signed_blob_id"=>"xxxxxx", "variation_key"=>"xxxxxx", "filename"=>"steven"}
  ActiveStorage::Blob Load (0.3ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" =  LIMIT   [["id", 3], ["LIMIT", 1]]
  ↳ /Users/stevenaguilar/.rvm/gems/ruby-2.2.2/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
  Disk Storage (0.1ms) Checked if file exists at key: variants/7rnyyMpZaqXT4RBNtzDqPFqS/477efe2eb62003af0b5b40ec71c56de636f58f942964d830feeed4057b8718a6 (no)
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.3ms)

如果我编辑为 <%= image_tag @profile.avatar %>,我可以看到原始图像。

这里是 Photo 模型:

 class Profile < ApplicationRecord
  belongs_to :user
  has_one_attached :avatar
 end

有什么建议吗?

试试这个:

<%= image_tag @profile.avatar.variant(resize: "100x100") %>

您使用的Rails版本为5.2,该版本的ActiveStorage没有variant_processor选项。

resize_to_fit是属于image_processing的选项。您安装 gem,但处理器固定为 mini_magick。参见:https://github.com/rails/rails/blob/v5.2.3/activestorage/app/models/active_storage/variant.rb#L117

如果您想保留 5.2 版本,您应该遵循 mini_magick 提供的选项。参见:https://api.rubyonrails.org/classes/ActiveStorage/Variation.html

如果您更喜欢使用 image_processing 享受方便的选择,您应该将 Rails 版本升级到 >= 6。