gpt4 book ai didi

ruby-on-rails - URI::InvalidURIError(错误的URI(不是URI?):无)Active Storage service_url

转载 作者:行者123 更新时间:2023-12-03 14:47:56 27 4
gpt4 key购买 nike

配置信息

rails version 6.0
ruby version 2.7.0
gem 'image_processing', '~> 1.2'

storage.yml
local:
service: Disk
root: <%= Rails.root.join("storage") %>

development.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 }

仅当我使用service= :local时才会出现此异常。 在使用 :amazon时可以正常工作。

user.rb 模型
  has_one_attached :avatar

#throwing exception
def avatar_urls
{
original: avatar.service_url
} if avatar.attachment
end

访问 avatar_urls时会引发异常( URI::InvalidURIError (bad URI(is not URI?): nil))。但是,正如我将 avatars_url方法更改为following一样,它可以正常工作。
  #working method
def avatar_urls
{
thumbnail: url_for(avatar.variant(resize: "100x100").processed)
} if avatar.attachment
end

这是跟踪:
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

最佳答案

我在Rails 6.1中遇到了同样的问题,发现我必须在 Controller 中包括一个特殊的问题,以使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

关于ruby-on-rails - URI::InvalidURIError(错误的URI(不是URI?):无)Active Storage service_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60425407/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com