1

Ruby 2.7 Rails 7.0.3.1

/home/felipe/.rvm/gems/ruby-2.7.1/gems/zeitwerk-2.6.0/lib/zeitwerk/loader.rb:359:in `rescue in block in set_autoloads_in_dir': wrong constant name Usersession.controller inferred by Module from file (Zeitwerk::NameError)

Possible ways to address this:

  • Tell Zeitwerk to ignore this particular file.
  • Tell Zeitwerk to ignore one of its parent directories.
  • Rename the file to comply with the naming conventions.
  • Modify the inflector to handle this case.

usersession.controller.rb:

    class SessionsController < ApplicationController

    def new
    redirect_to user_contacts_path(current_user) if user_signed_in?
    end

    def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
    sign_in(user)
    redirect_to user_contacts_path(current_user)
    else
    flash.now[:danger] = 'Email e Senha inválidos'
    render 'new'
    end
     end

     def destroy
    sign_out
    flash[:warning] = 'Logout realizado com sucesso'
    redirect_to entrar_path
     end


    end

application.rb :

      require_relative "boot"
      require "rails/all"

      # Require the gems listed in Gemfile, including any gems
      # you've limited to :test, :development, or :production.
      Bundler.require(*Rails.groups)

      module App
        class Application < Rails::Application
        # Initialize configuration defaults for originally generated Rails version.
        config.load_defaults 7.0
        # Configuration for the application, engines, and railties goes here.
        #
        # These settings can be overridden in specific environments using the files
        # in config/environments, which are processed later.
        #
        # config.time_zone = "Central Time (US & Canada)"
        # config.eager_load_paths << Rails.root.join("extras")
           end
         end
TrindadeF
  • 11
  • 3

1 Answers1

3

It seems to me that the problem you're facing is because the filename of the controller is different from the class: userssesions.controller != SessionsController. Zeitwerk uses the name of the file to find the class. You have two options:

  1. Rename the file to sessions_controller.rb
  2. Rename SessionsController to UsersessionController. PS: remember to replace "." with "_" in the filename. Ex: usersessions_controller, instead of usersesions.controller.

After doing that, restart your server.

Why this error is happening? Because we're not following Zeitwerk convention:

To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define.

Example:

lib/my_gem.rb         -> MyGem
lib/my_gem/foo.rb     -> MyGem::Foo
lib/my_gem/bar_baz.rb -> MyGem::BarBaz
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo

Reference: https://github.com/fxn/zeitwerk#file-structure

Pedro Paiva
  • 721
  • 11
  • 17