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
Related
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.
I overwritten Devise sessioncontroller#create as it is in the tutorial (SessionController < Devise::SessionController and adding devise_for in routes) but the session is still made by gem. Does anyone know what to do in such a situation?
Now you have few problems:
There is no such Devise::SessionController constant (Devise::SessionsController is present)
In devise_for :users, controllers {sessions: 'sessions'} you doesn't use :controllers key and specify absent controller (sessions instead of session)
Usually controllers are plural
How you can fix it
Rename SessionController < Devise::SessionController to SessionsController < Devise::SessionsController (in app/controllers/sessions_controller.rb)
Specify this controller in routes.rb as devise_for :users, controllers: {sessions: 'sessions'}
Read more
I am using Devise 3.1.1 and am trying to redirect user to the Sign In page after he signs up.
As instructed in Devise's wiki I overridden RegistrationsController with the following:
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
'/users/sign_in'
end
end
As instructed, I also added the following line to the routes.rb:
devise_for :users, controllers: { registrations: 'registrations'}
After which I get the following error when I go to sign in page:
Invalid route name, already in use: 'new_user_session' You may have defined two routes with the same name using the:asoption, or you may be overriding a route already defined by a resource with the same naming.
In my routes I already have this defined:
devise_for :users, skip: :registrations
devise_scope :user do
resource :registration,
# disabled :edit & :destroy
only: [:new, :create, :update],
path: 'users',
path_names: { new: 'sign_up' },
controller: 'devise/registrations',
as: :user_registration do
get :cancel
end
end
You can only define the devise_for block once, and as you're already messing with the default registrations controller you should be able to just do something like the following to have devise use your controller:
devise_for :users, skip: :registrations
devise_scope :user do
resource :registration,
# disabled :edit & :destroy
only: [:new, :create, :update],
path: 'users',
path_names: { new: 'sign_up' },
controller: 'registrations',
as: :user_registration do
get :cancel
end
end
I need my Rails app to redirect to the home page after I submit the email to send me reset password instructions. Devise, by default renders the sign in form after entering the email.
So I am trying to override the Devise::PasswordsController and change its redirect_to, but no success. In fact, I don't think Rails is even taking in my class. It could be a very stupid mistake but I have been at it for half a day with no success.
I took the idea to override the passwords controller from here.
Here's my controller:
class PasswordsController < Devise::PasswordsController
protected
def after_sending_reset_password_instructions_path_for(resource_name)
root_url
end
end
Routes.rb:
devise_for :users, :controllers => {:passwords => "passwords"}
devise_for :users, :controllers => {:registrations => "registrations"}
devise_for :users, :controllers => {:sessions => "sessions"}
I would like to mention that I have overridden Devise's Registations and Sessions Controllers in the same app, and they seem to work fine.
It should be possible to override the controller with the latest version of Devise (2.1.2).
class PasswordsController < Devise::PasswordsController
def new
super
end
def create
..override method here..
end
end
And in config/routes.rb:
devise_for :users, controllers: { passwords: 'passwords', .. }
You can check with rake routes if Rails uses the derived PasswordsController instead of the original one, the routes should for instance contain passwords#new instead of devise/passwords#new.
I think you forgot to mention your changes in the routes:
devise_for :users, :controllers => {:sessions => "sessions", :passwords => "passwords"}
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" }