首页 > 解决方案 > ActiveStorage::InvariableError 使用 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" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["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" = $1 AND "accounts"."id" IS NULL LIMIT $2  [["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" = $1 ORDER BY "accounts"."created_at" ASC LIMIT $2  [["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" = $1 LIMIT $2  [["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" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["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" = $1 LIMIT $2  [["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

标签: ruby-on-railsrubyrails-activestorage

解决方案


这解决了我的问题

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

推荐阅读