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

时间:2018-09-26 20:36:54

标签: ruby-on-rails rails-activestorage ruby-on-rails-5.2

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

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

我得到以下内容enter image description here

我看不到图片。

我在自己的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" = $1 LIMIT $2  [["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

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

尝试一下:

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

答案 1 :(得分:0)

您使用的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。