Rails raises exception when config.active_storage.service is not explicitly set

ActiveStorage makes it easy for developers to upload files to cloud services like Amazon S3, Google Cloud Storage, and more. It also provides a local disk service for development and testing. Rails reads the config value config.active_storage.service to determine which service to use. If this value is not set, Rails will raise an exception.

However, when the config.active_storage.service is not set, the application raises a vague error causing unnecessary time lost to debugging.

Before

This value is set in the environment file via the rails active_storage:install command. However, in some cases it can be removed or commented out.

Let’s use a simple example to demonstrate this. We have a User model with an avatar attachment.

  class User < ApplicationRecord
    has_one_attached :avatar
  end

When we try to upload an avatar, we get the following error:

  /Users/swaathi/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/activestorage-7.0.4/app/models/active_storage/blob.rb:90:in `build_after_unfurling': missing keyword: :filename (ArgumentError)   

This is a cryptic error that does not give us any indication of what the problem is. It takes significant effort to figure out that the config.active_storage.service is not set.

After

Thanks to this PR ActiveStorage gives much more descriptive errors when the config.active_storage.service is not set.

  /Users/swaathi/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rails-1d849dfbe8f0/activestorage/lib/active_storage/attached/model.rb:226:in `validate_global_service_configuration': Missing Active Storage service name. Specify Active Storage service name for config.active_storage.service in config/environments/development.rb (RuntimeError)

Need help on your Ruby on Rails or React project?

Join Our Newsletter