URI::InvalidURIError(错误的 URI(不是 URI?):nil)Active Storage service_url

Imr*_*mad 11 ruby-on-rails rails-activestorage ruby-on-rails-6

配置信息

rails version 6.0
ruby version 2.7.0
gem 'image_processing', '~> 1.2'
Run Code Online (Sandbox Code Playgroud)

存储.yml

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>
Run Code Online (Sandbox Code Playgroud)

发展.rb

config.active_storage.service = :local
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Run Code Online (Sandbox Code Playgroud)

只有当我使用service= :local. 使用 AWS S3 配置,使用:amazon它可以正常工作。

用户.rb模型

  has_one_attached :avatar
  
  #throwing exception
  def avatar_urls
    {
      original: avatar.service_url
    } if avatar.attachment
  end
Run Code Online (Sandbox Code Playgroud)

访问时抛出avatar_urls异常( URI::InvalidURIError (bad URI(is not URI?): nil) )。但是,如果我将avatars_url方法更改为以下方法,则效果很好。

  #working method
  def avatar_urls
    {
      thumbnail: url_for(avatar.variant(resize: "100x100").processed)
    } if avatar.attachment
  end
Run Code Online (Sandbox Code Playgroud)

这是跟踪:

Disk Storage (5056.7ms) Generated URL for file at key: variants/i5w1ie6ro07mib4qcdn30lmik6wn/2a7fa5dad6ac227a16e961cbd12ca6f35f1d7947f56a97754d5e22c1a0fd3372 ()
cb_app_container | Completed 500 Internal Server Error in 9523ms (ActiveRecord: 580.9ms | Allocations: 1185509)
cb_app_container | URI::InvalidURIError (bad URI(is not URI?): nil):
cb_app_container | app/models/user.rb:53:in `avatar_urls'
cb_app_container | app/models/user.rb:27:in `user_json'
cb_app_container | app/controllers/api/v1/users_controller.rb:13:in `update'
cb_app_container | [ActiveJob] [ActiveStorage::AnalyzeJob] [33448db4-cf54-4677-906c-06b59f1579ee]    (61.6ms)  BEGIN
cb_app_container | [ActiveJob] [ActiveStorage::AnalyzeJob] [33448db4-cf54-4677-906c-06b59f1579ee]   ActiveStorage::Blob Update (10.3ms)  UPDATE "active_storage_blobs" SET "metadata" = $1 WHERE "active_storage_blobs"."id" = $2  [["metadata", "{\"identified\":true,\"width\":1952,\"height\":3264,\"analyzed\":true}"], ["id", 45]]
cb_app_container | [ActiveJob] [ActiveStorage::AnalyzeJob] [33448db4-cf54-4677-906c-06b59f1579ee]    (19.6ms)  COMMIT
cb_app_container | [ActiveJob] [ActiveStorage::AnalyzeJob] [33448db4-cf54-4677-906c-06b59f1579ee] Performed ActiveStorage::AnalyzeJob (Job ID: 33448db4-cf54-4677-906c-06b59f1579ee) from Async(default) in 6916.06ms
Run Code Online (Sandbox Code Playgroud)

小智 9

我在 Rails 6.1 中遇到了同样的问题,发现我必须在我的控制器中包含一个特殊的问题来ActiveStorage了解当前主机:

module Api
  module V1
    class ApiController < ActionController::API
      # Make ActiveStorage aware of the current host (used in url helpers)
      include ActiveStorage::SetCurrent
    end
  end
end

Run Code Online (Sandbox Code Playgroud)


小智 4

我最终将其包含include Rails.application.routes.url_helpers在控制器中并使用了rails_blob_url(avatar.image). 我还在路由文件中添加了这一行。稍后我会重构它。

Rails.application.routes.default_url_options[:host] = 'localhost:3000'