Superclass mismatch for class RegistrationController - Overriding Devise Controller - ruby-on-rails

I am trying to override my devise registration controller, with no luck.
I finally got the routes working, but now I'm getting an superclass mismatch for class error.
Heres my setup:
Registration Controller (app/controllers/users/registrations_controller.rb)
class RegistrationsController < Devise::RegistrationsController
def sign_up_params
devise_parameter_sanitizer.sanitize(:sign_up)
params.require(:user).permit(:email, :password, profile_attributes: [:username])
end
def new
super
end
def create
end
def update
super
end
end
Routes
root 'welcome#index'
devise_for :users, :controllers => {:registrations => "users/registrations"}
Views
--edit.html.erb && new.html.erb exist in the folder (app/views/users/registrations)
User Model (just in case)
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile
accepts_nested_attributes_for :profile
def profile
super || build_profile
end
end
Any idea why this error is appearing?
Thanks!

Your controller is underneath the users directory but does not have a Users module (it is not in the Users namespace, you might say). Either change the controller to this:
module Users
class RegistrationsController < Devise::RegistrationsController
...
end
end
Or move your controller up a directory
app/controllers/registrations_controller.rb

Define your RegistrationsController as below
class Users::RegistrationsController < Devise::RegistrationsController
...
end
By defining the controller as suggested above, you don't need to define the module explicitly.
You get the error because you have placed the RegistrationsController inside users folder.
So, rails expects that RegistrationsController is a class belonging to Users module.

Related

Edit Registrations/Edit in Devise

Started to learn and develop rails - while it is pretty straight forward I got stuck changing the implementation of the /users/edit view and endpoint in the default configuration of devise.
My approach is pretty simple, all routes and logic for authentication, password resetting etc. get handled by devise and it's routes. My problem is that I want users to change their passwords and email via my custom /settings route and view. For me /users/edit just doesn't make sense and I want to remove it from the accessible routes and ideally use its logic for my custom route.
routes.rb
Rails.application.routes.draw do
devise_for :users
# Defines the root path route ("/")
root "home#index"
# SETTINGS
get "settings/profile" => "settings#profile"
get "settings/account" => "settings#account"
get "settings/billing" => "settings#billing"
get "settings/notifications" => "settings#notifications"
end
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :lockable,
:trackable, :omniauthable
def remember_me
true
end
end
settings_controller.rb
class SettingsController < ApplicationController
before_action :authenticate_user!
def profile
end
def account
end
def billing
end
def notifications
end
end

Before Action Authenticate with Devise and STI not working

I'm trying to use before_action :authenticate_admin! to determine if the current user is an admin or not. I am using devise with single table inheritance (STI).
Here is my User model;
class User < ApplicationRecord
def admin?
false
end
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :lockable
end
And my Admin model;
class Users::Admin < User
def devise_scope
:admin
end
def admin?
true
end
end
Just to test out devise, I have this in my routes file;
authenticated :user, ->(u) { u.admin? } do
resources :teams
end
And indeed, if I am signed in as a different User type (so not Admin), I can't get to that resource.
But this fails to work;
class Teams < ApplicationController
before_action :authenticate_admin!
def index
end
end
I get the error;
undefined method `authenticate_admin!'
Quite new to Rails so I don't understand how to resolve this.
UPDATE
I had to put this in the routes file;
devise_for :admins, class_name: 'Users::Admin'
Previously I only had;
devise_for :users, controllers: {sessions: 'users/sessions'}

change password for devise admin without :registerable

I want to make Devise Admin not registerable, but when I take :registerable away from admin.rb, the edit_admin_registration_path does not work. In this case, how can an admin change his email/password?
class Admin < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :timeoutable, :registerable
Error:
undefined local variable or method `edit_admin_registration_path' for #<#<Class:0x8cf7b40>:0x8cf5278>
Note: I have 2 different Devise Models
Removing :registerable from your Admin model will remove all route helpers for the Devise::RegistrationsController, such as: edit_admin_registrations_path.
You will need to keep the :registerable in your Admin model and override the Devise::RegistrationsController to restrict access to new and create actions.
Create a new registrations_controller.rb that contains:
class RegistrationsController < Devise::RegistrationsController
def new
redirect_to root_path
end
def create
redirect_to root_path
end
end
And in your routes.rb tell Devise to use your registrations controller.
devise_for :admins, controllers: { registrations: 'registrations' }
This way you will restrict users from creating accounts, but allow existing ones to edit their info.

creating sign in only views/methods

I created a user model using devise. I was wondering how can I create views and methods that only work after I've signed in?
Here is my model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:rememberable, :validatable
end
controller:
class UserController < ApplicationController
def create
User.create(user_params)
end
private
def user_params
# required input for params
# permit - returns a version of the params hash with ony the permitted attributes
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
You will be protecting controllers and it's actions.
before_action :authenticate_user!
user_signed_in?
current_user
user_session
read more!
https://github.com/plataformatec/devise

Rails undefined method `find_or_create_from_auth_hash'

My sessions_controller is as follows:
class SessionsController < ApplicationController
require 'omniauth-facebook'
require 'omniauth'
def create
#user = User.find_or_create_from_auth_hash(auth_hash)
self.current_user = #user
redirect_to '/'
end
protected
def auth_hash
request.env['omniauth.auth']
end
end
So... it's THERE isn't it?
Here's my users.rb file:
class User < ActiveRecord::Base
acts_as_voter
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :posts
has_many :tasks
end
And my routes file:
LiquidAdmin::Application.routes.draw do
devise_for :users
get '/auth/:provider/callback', to: 'sessions#create'
resource :sessions, :only => :create
get "home/bandwall"
get "home/index"
root :to => "home#index"
So where's the problem? "auth_hash" is clearly defined... The SessionsController is loading... so why is it complaining about no method for find_or_create_from_auth_hash?
In Rails 3.2, the method you'll want to use is called first_or_create:
User.where(auth_hash: auth_hash).first_or_create
According to the Rails 3.2 release notes:
Add first_or_create, first_or_create!, first_or_initialize methods to
Active Record. This is a better approach over the old
find_or_create_by dynamic methods because it's clearer which arguments
are used to find the record and which are used to create it.
Given this, the following is made possible through the query:
User.where(auth_hash: auth_hash).first_or_create(foo: 'bar') # Assuming no other User entries exist
#=> #<User id: 1, auth_hash: "your_auth_hash", foo: "bar">
User.where(auth_hash: auth_hash).first_or_create(foo: 'baz')
#=> #<User id: 1, auth_hash: "your_auth_hash", foo: "bar">
Given that this question is based on OmniAuth, I think a better answer would be:
#user = User.where(auth_hash).first_or_create
or if in Rails version greater than 4.1.14
#user = User.where(uid: auth_hash[:uid], provider: auth_hash[:provider]).first_or_create
auth_hash is not a field in the User model, but rather a Hash of fields that could be on the User model.

Resources