[RUBY] Rails uninitialized constant A simple checklist for error resolution

What is this

Despite doing Rails as it is, I'm addicted to uninitialized constant errors, so I'll put together a checklist here.

Conclusion

The following three items should be checked.

  1. Does the file name and class name match? --Does the file path and module definitions match? --Are Rails reading the file?

I will write the details below.

Does the file name and class name match?

If the following class exists, the file name must be qiita_user.rb.

class QiitaUser
  def hoge
  end
end

Do the file path and module definitions match?

If there is a nested class in the following module, the path will be something like ** / v1 / auth / user.rb.


class V1::Auth::User
end

OR


module V1
  module Auth
    class User
    end
  end
end

Are you letting Rails read the file?

If you want to add a folder other than the Rails default folder, you need to define the path to the added folder in autoload path. Suppose you add ʻapp / lib / hoge / foo.rb. In this case, you need to add the autoload path to config / application.rb` in the following form.

module App
  class Application < Rails::Application
    #abridgement
    
    config.autoload_paths += Dir.glob("#{config.root}/app/lib")
  end
end

Recommended Posts

Rails uninitialized constant A simple checklist for error resolution
Uninitialized constant ~~ error
rails error resolution summary
Remedy for "A server is already running." Error when running rails s
Define multiple names for a model NameError (uninitialized constant User :: Reserf):
[Rails withdrawal] Create a simple withdrawal function with rails
Testing for Error Messages: Rails Tutorial Notes-Chapter 7
[Ruby / Rails] Mechanism for retrying Thread Error
[Rails] Let's create a super simple Rails API
A simple and convenient method for HashMap
[Rails / Heroku] Error resolution procedure after push
Dealing with NameError: uninitialized constant :: Analyzable error when installing Active Storage in Rails6
Build a development environment for Docker + Rails6 + Postgresql
[Rails] Error resolution when generating tokens with PAYJP
A collection of simple questions for Java beginners
[For beginners] Procedure for creating a controller using rails
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese