2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rails6】Initialization autoloaded the constants ApplicationHelper を解消する

Last updated at Posted at 2022-08-08

初めに

Rails6 で Rspec を稼働させるとDEPRECATION WARNINGが出るので、それを解決します。

解決方法

エラー内容

以下のエラーが発生しました。

DEPRECATION WARNING: Initialization autoloaded the constants ApplicationHelper,DeviseHelper、ApplicationController、
DeviseController、Devise::SessionsController、Devise::PasswordsController、Devise::UnlocksController、Devise::RegistrationsController、
Devise::ConfirmationsController.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload CustomErrors, for example,
the expected changes wont be reflected in that stale Module object.

These autoloaded constants have been unloaded.

In order to autoload safely at boot time, please wrap your code in a reloader
callback this way:

    Rails.application.reloader.to_prepare do
      # Autoload classes and modules needed at boot time here.
    end

That block runs when the application boots, and every time there is a reload.
For historical reasons, it may run twice, so it has to be idempotent.

Check the "Autoloading and Reloading Constants" guide to learn more about how
Rails autoloads and reloads
(called from <top (required)> at /coffee-oma/config/environment.rb:5)

config/initializers からControllerクラスなどを呼び出しでエラーが発生します。

解決例

まずは、どのファイルから呼び出されているかを確認する必要があります。

今回はApplicationHelperpp caller_locationsを使い、どのファイルで呼び出されているかをコンソールに出力させます。

application_helper.rb
module ApplicationHelper
  pp caller_locations.select { |l| l.to_s.index("config/initializers") }
  # 以下省略
end

これでコンソールに呼び出されたファイル名が出力されるので、そのクラスをRails.application.reloader.to_prepareでラップさせると解決できます。

config/initializers/devise.rb
Rails.application.reloader.to_prepare do
  ActiveAdmin::Devise::SessionsController.class_eval do
    def after_sign_in_path_for(resource)
      if resource.is_a?(AdminUser)
        admin_root_path
      else
        root_path
      end
    end
  end
end

参考

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?