uninitialized constant Devise::SessionsController (NameError) - ruby-on-rails

i updated the app i am working with with ruby 3.1.2, and rails 7.0.3.1, before everything worked but now i get this error, which i believe is due to the file created for active_admin.
this is the active_admin file
class ActiveAdmin::Devise::SessionsController
include ::ActiveAdmin::Devise::Controller
def create
super
resource.login_histories.create(ip: request.remote_ip)
end
end
the routes
Rails.application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions' }
devise_scope :user do
root 'users/sessions#new'
end
end
Here is the SessionsController
class Users::SessionsController < Devise::SessionsController
def new
super
end
def create
super
resource.login_histories.create(ip: request.remote_ip)
end
end
I am a beginner any help will be appreciated. thank you.
Mik.

Related

Uninitialized Controller Constant in Ruby on Rails 5

I am getting a uninitialized constant Project::Controller. I've looked through the Rails docs and posts here on SO, but the code seems to be setup correctly. I am using rails 5.1.1. My pages path works fine, only the root path gives the error.
routes .rb
Rails.application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users,
path: '',
path_names: {
sign_in: 'login',
sign_out: 'logout',
edit: 'profile'
},
controllers: { omniauth_callbacks: 'omniauth_callbacks' }
get 'pages/about'
root 'project/#index'
resources :project do
resources :task, only: [:show]
end
end
project_controller.rb
class ProjectController < ApplicationController
def index
#projects = Project.all
end
def show
#project = Project.find(params[:id])
#tasks = #project.tasks
end
end
Change
root 'project/#index'
to
root 'project#index'

Rails: Routing Error uninitialized constant RegistrationsController

I am trying to implement devise authentication using ajax and jquery. I generated devise controllers using:
rails g devise:controllers users It created all the necessary controller files required such as sessions, registration.
These are the controller files:
class Users::SessionsController < Devise::SessionsController
respond_to :html, :json
end
class Users::RegistrationsController < Devise::RegistrationsController
respond_to :html, :json
end
routes.rb file looks like below:
devise_for :users, controllers: {sessions: 'sessions', registrations: 'registrations'}
As soon as I fill the registration form and hit enter I get:
Routing Error
uninitialized constant RegistrationsController
You're missing the namespace in your routes
devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'}
change routes.rb from
devise_for :users, controllers: {sessions: 'sessions', registrations: 'registrations'}
to
devise_for :users, controllers: {sessions: 'users/sessions', registrations: 'users/registrations'}
I experienced this issue when working on a Rails 6 application.
I had my model named application_registration.rb
My views directory was app/views/application_registrations
And my routes was: resources :application_registrations, only: [:create, :update, :new, :edit]
But it was throwing an error:
uninitialized constant ApplicationRegistrationsController Did you mean ApplicationController
Here's how I fixed it:
The issue arose from the naming of my controller.
I had named it application_registrations.rb
All I had to do was to rename it to application_registrations_controller.rb
That's all
I hope this helps

Cannot login/logout with Devise, because "uninitialized constant Users::SessionsController"

I have following setup in routes.rb:
devise_for :users, path_names: { sign_in: "login", sign_out: "logout" },
controllers: { omniauth_callbacks: "authentications", registrations: "users/registrations", :sessions => "users/sessions" }
devise_scope :user do
... some other routes ...
end
When I try to sign up, log in or log out, I always get a similar error message like this:
Routing Error
uninitialized constant Users::SessionsController
In controllers, I have a folder called users and in this folder is registrations_controller.rb with following content:
class Users::RegistrationsController < Devise::RegistrationsController
def create
super
session[:omniauth] = nil unless #user.new_record?
end
def build_resource(*args)
super
if session[:omniauth]
#user.apply_omniauth(session[:omniauth])
#user.valid?
end
end
end
How to get rid of that error message?
Thank you.

Sharing RegistrationController for two devise models

I have two devise models, Individual and Group. I'm trying to share similar behavior for Registrations/Confirmations (send users to a page that informs them that an email has been sent, or send them to the edit page after confirmation, etc.)
My issue is that this works for Individuals, only. It has yet to work for Groups.
config/outes.rb:
devise_for :individuals, :controllers => {:confirmations => "Confirmation", :registrations => "Registrations"}
devise_for :groups, :controllers => {:confirmations => "Confirmation", :registrations => "Registrations"}
app/controllers/registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController
def after_inactive_sign_up_path_for(item)
"/post_sign_up?email=#{item.email}"
end
end
app/controllers/confirmation_controller.rb:
class ConfirmationController < Devise::ConfirmationsController
def after_confirmation_path_for(name, resource)
case resource.class
when Individual then edit_individual_path resource
when Group then edit_group_path resource
else super name, resource
end
end
end
The above code works for individuals, only. Can't figure out why though.
I was focused so heavily on the provided lines in routes.rb, I failed to notice another "devise_for :groups" at the top of the file. Apparently devise will override previous values in this case. Stupid mistake.
for scale & no errors - better to devide.
admin_registration_controller.rb:
class AdminRegistrationsController < Devise::RegistrationsController
end
user_registration_controller.rb:
class UserRegistrationsController < Devise::RegistrationsController
end
routes:
devise_for :admins, controllers: { registrations: 'admin_registrations' }
devise_for :users, controllers: { registrations: 'user_registrations' }

Devise custom registrations controller error

When I try to create a custom devise controller:
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
# add custom create logic here
end
def update
super
end
end
I get a following error:
Unknown action
AbstractController::ActionNotFound
It is not the problem with routes. I tried to inherit RegistrationsController from ApplicationController and it works fine. As soon as i try to inherit from Devise::RegistrationsController it shows an error. It can't be an action problem to, becuse I tried to create a different action, and I get the same error.
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
root :to => "registrations#new"
Using Rails 3.0.4
In your routes you have to use devise_scope if you are overriding devise default actions.
devise_for :users, :controllers => {:registrations => "registrations"}
devise_scope :user do
root :to => "registrations#new"
end
For a similar issue please see http://groups.google.com/group/plataformatec-devise/browse_thread/thread/a5beaaf4b1ad343a
Also here are the docs on changing default sign in routes, I know this you are doing registration, but this could be similar: https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
I used the following code in my project successfully:
app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
end
routes.rb
devise_for :users, :controllers => { :registrations => "users/registrations" }

Resources