Config Info
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 }
This exception is coming only when I'm using service= :local
. With AWS S3 config, on using :amazon
it works fine.
user.rb model
has_one_attached :avatar
#throwing exception
def avatar_urls
{
original: avatar.service_url
} if avatar.attachment
end
While accessing avatar_urls
the exception(URI::InvalidURIError (bad URI(is not URI?): nil)
) is thrown. However, as If I change my avatars_url
method to following, it works fine.
#working method
def avatar_urls
{
thumbnail: url_for(avatar.variant(resize: "100x100").processed)
} if avatar.attachment
end
Here is the trace:
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
I ran into this same issue in Rails 6.1, and found that I had to include a special concern in my controller to make ActiveStorage
aware of the current host:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With