Authlogic NameError - wrong constant name Object.const_get(camel_cased_word)

2.2k Views Asked by At

I am trying setup Authlogic gem with rails 7. I have simple controller:

class UsersController < ApplicationController
  def new
    render_page 'Registration'
  end

  def create
    @user = User.new(email: '[email protected]', password: 415561)
    if @user.save
      redirect_to root_url
    else
      render_page 'Registration', props: { errors: @user.errors_codes }
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :surname, :email, :password, :password_confirmation).to_h
  end
end

and simple User model:

class User < ApplicationRecord
  self.inheritance_column = 'there_is_no_sti_here'

  acts_as_authentic do |c|
    c.session_class = Session
    c.crypto_provider = ::Authlogic::CryptoProviders::BCrypt
  end
end

But when I call create action of UsersController, I get error:

NameError - wrong constant name 
      Object.const_get(camel_cased_word)
            ^^^^^^^^^^:
  app/controllers/users_controller.rb:8:in `create'

That error refers to @user.save in UsersController. I can not figure out what is happenning. I tried to debug this line, but did not understand what is going on. Also I tried authlogic gem right from the GitHub repository, but it didn't help.

The most interesing part that all works as intended when I create new instnce of User and then save it from rails console...

1

There are 1 best solutions below

0
faron On

Looks like it's failing here:

@klass ||= klass_name ? klass_name.constantize : nil

When it tries to get klass_name from session_class you've set:

# The model name, guessed from the session class name, e.g. "User",
# from "UserSession".
#
# TODO: This method can return nil. We should explore this. It seems
# likely to cause a NoMethodError later, so perhaps we should raise an
# error instead.
#
# @api private
def klass_name
  return @klass_name if instance_variable_defined?(:@klass_name)
  @klass_name = name.scan(/(.*)Session/)[0]&.first
end

In this case you should either configure authenticate_with User or rename Session to UserSession so it could get the correct name.