ActiveStorage::InvariableError 使用 Mini Magick

ActiveStorage::InvariableError using Mini Magick

<%= image_tag @supply.supply_photo.variant(resize: "400x400") %>

这是我的看法。 @supply.supply_photo 适用于未调整大小的原始图像。我的图像是一个 webp...我把它变成了一个具有相同结果的 jpg。我觉得我错过了一些简单的东西,但可以弄清楚我错过了什么。

我得到的错误是ActiveStorage::InvariableError

我的日志显示

Started PUT "/__web_console/repl_sessions/cbaf86fd1070dd817c45101a6cf549f0" for ::1 at 2020-08-09 19:21:08 -0700
Started PUT "/__web_console/repl_sessions/cbaf86fd1070dd817c45101a6cf549f0" for ::1 at 2020-08-09 19:21:09 -0700
Started GET "/supplies/1" for ::1 at 2020-08-09 19:21:57 -0700
Processing by SuppliesController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" =  ORDER BY "users"."id" ASC LIMIT   [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/concerns/set_current_request_details.rb:11:in `block (2 levels) in <module:SetCurrentRequestDetails>'
  Account Load (0.8ms)  SELECT "accounts".* FROM "accounts" INNER JOIN "account_users" ON "accounts"."id" = "account_users"."account_id" WHERE "account_users"."user_id" =  AND "accounts"."id" IS NULL LIMIT   [["user_id", 2], ["LIMIT", 1]]
  ↳ app/controllers/concerns/set_current_request_details.rb:24:in `block (2 levels) in <module:SetCurrentRequestDetails>'
  Account Load (0.8ms)  SELECT "accounts".* FROM "accounts" INNER JOIN "account_users" ON "accounts"."id" = "account_users"."account_id" WHERE "account_users"."user_id" =  ORDER BY "accounts"."created_at" ASC LIMIT   [["user_id", 2], ["LIMIT", 1]]
  ↳ app/controllers/concerns/set_current_request_details.rb:29:in `block (2 levels) in <module:SetCurrentRequestDetails>'
  Supply Load (0.5ms)  SELECT "supplies".* FROM "supplies" WHERE "supplies"."id" =  LIMIT   [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/supplies_controller.rb:51:in `set_supply'
  Rendering supplies/show.html.erb within layouts/application
  ActiveStorage::Attachment Load (0.4ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" =  AND "active_storage_attachments"."record_type" =  AND "active_storage_attachments"."name" =  LIMIT   [["record_id", 1], ["record_type", "Supply"], ["name", "supply_photo"], ["LIMIT", 1]]
  ↳ app/views/supplies/show.html.erb:17
  ActiveStorage::Blob Load (0.4ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" =  LIMIT   [["id", 3], ["LIMIT", 1]]
  ↳ app/views/supplies/show.html.erb:17
  Rendered supplies/show.html.erb within layouts/application (Duration: 7.3ms | Allocations: 2886)
Completed 500 Internal Server Error in 25ms (ActiveRecord: 3.6ms | Allocations: 6842)


  
ActionView::Template::Error (ActiveStorage::InvariableError):
    14:         <h5>Url</h5>
    15:         <%= @supply.url %>
    16:       </div>
    17:       <%= image_tag @supply.supply_photo.variant(resize_to_limit: "400x400") %>
    18:     </div>
    19:   </div>
    20: </div>
  
app/views/supplies/show.html.erb:17

这解决了我的问题

<%= image_tag @supply.supply_photo.variant(resize_to_limit: [400, 400]) %>

在你的配置文件中 config/environments/production.rb 添加这一行

config.active_storage.variable_content_types = %w(image/png image/gif image/jpg image/jpeg image/webp)

并在 ubuntu 安装 webp 应用程序

sudo apt-get install webp

这对我有用