DEV Community

Cover image for How to continue to use Rails.application.secrets in Rails >= 7.2
Artem Yegorov
Artem Yegorov

Posted on

How to continue to use Rails.application.secrets in Rails >= 7.2

You could notice that in Rails 7.1 shown warning message in logs:

W, [2024-10-21T15:34:51.142815 #472855]  WARN -- : DEPRECATION WARNING: `Rails.application.secrets` is deprecated in favor of `Rails.application.credentials` and will be removed in Rails 7.2.
Enter fullscreen mode Exit fullscreen mode

Yes, of course, you can transfer all the contents of the file config/secrets.yml to config/credentials/production.yml.enc by using

EDITOR=vim rails credentials:edit --environment production

But as for me, for an existing project, I want to stay on a simpler storage technology for configuration. In order to continue using the storage of configuration in the file config/secrets.yml.
You only have to add one line to the configuration in your rails app:

# your config/application.rb file
module MyApp
  class Application < Rails::Application
    # ...
    config.secrets = config_for(:secrets)
    # ...
  end
end
Enter fullscreen mode Exit fullscreen mode

Next, replace all your appeals to Rails.application.secrets with Rails.configuration.secrets, e.g.:

class MyController < ApplicationController
  def index
    # my_var = Rails.application.secrets.dig(:my_config, :my_var)
    my_var = Rails.configuration.secrets.dig(:my_config, :my_var)
    # ...
  end
  # ...
end
Enter fullscreen mode Exit fullscreen mode

Some useful links:

If you have comments and suggestions, I will gladly read them in the comments.
Thank you for reading, good day to you!

Top comments (0)