当前,我正在开发Rails 5.2应用程序。当我尝试使用show.html.erb
在variant
模板中显示个人资料的头像时,它不起作用
<%= 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" = $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
有什么建议吗?
答案 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。