How can I override "after_sign_up_path_for" in ActiveAdmin? - ruby-on-rails

I am building a Rails application (using ActiveAdmin and Devise) and I am trying to override the after_sign_up_path_for to change the redirection after signing up.
I followed this tutorial from devise but my RegistrationsController is never called. I guess it might work a little bit differently with ActiveAdmin.
I also tried other solution I found on stack overflow without any luck.
Here is my routes:
Rails.application.routes.draw do
devise_config = ActiveAdmin::Devise.config
devise_config[:controllers][:omniauth_callbacks] = 'users/omniauth_callbacks'
devise_config[:controllers][:registrations] = 'registrations'
devise_for :users, devise_config
ActiveAdmin.routes(self)
# other routes
end
And my RegistrationsController: (which is never called)
class RegistrationsController < ActiveAdmin::Devise::RegistrationsController
protected
def sign_up(_resource_name, _resource)
true
end
def after_sign_up_path_for(_resource)
root_url
end
end
Thanks for your help !
My project:
Rails 4.2.6
ActiveAdmin 1.0.0.pre2
Devise 3.5.9

ActiveAdmin don't use your RegistrationsController and can't use them. You can define that method on your ApplicationController or you can do it this way:
# conig/initializer/active_admin.rb
ActiveAdmin::Devise::RegistrationsController.class_eval do
def after_sign_up_path_for(_resource)
root_url
end
end

Related

Rails Authentification with Devise: Redirect to Sign-In form after Sign-Up

I'm building an authentication using Devise.
Creating users works, but after the creation the user is automatically logged-in and becomes redirected to the root-page.
I have created an own RegistrationController and overwritten the after_sign_up_path method:
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
"/users/sign_in"
end
end
But it doesn't work.
What have I done wrong, respectively what have I missed.
I have read various other StackOverflow question already. Until now nothing has worked.
Here's my routes.rb too:
Rails.application.routes.draw do
resources :comments
resources :posts
devise_for :users, controllers: { registrations: "registrations" }
root "posts#index"
end
It seems like after you sign up successfully devise automatically log in and you cannot get login page if you still log in, correct me if I'm wrong
This is the default sign up method that ships with the RegistrationsController
# Signs in a user on sign up. You can overwrite this method in your own
# RegistrationsController.
def sign_up(resource_name, resource)
sign_in(resource_name, resource)
end
You can override it in your code.
Thanks to Nick M's answer I could solve the problem the following way:
First make sure your "routes.rb"-file has a line like here ...
Rails.application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
...
end
Then create a "registrations_controller.rb"-file and add there this:
class RegistrationsController < Devise::RegistrationsController
protected
def sign_up(resource_name, resource)
sign_out :user
end
end
Doing it that way results in being redirected to the Login-form automatically, after creating a user. Exactly the behaviour I liked to accomplish.
"#sign_out(resource_or_scope = nil) ⇒ Object
Sign out a given user or scope. This helper is useful for signing out a user after deleting accounts. Returns true if there was a logout and false if there is no user logged in on the referred scope
sign_out :user # sign_out(scope)
sign_out #user # sign_out(resource)"
Source

Rails Devise Confirmable - redirect

The Rails docs for devise say that the instructions for how to redirect are incorrect:
https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users#redirecting-user
Does anyone know how to do this?
you will need to override devise confirmation controller.
in your routes.rb add this line
devise_for :users, controllers: { confirmations: 'confirmations' }
create file in and paste this code
class ConfirmationsController < Devise::ConfirmationsController
private
def after_confirmation_path_for(resource_name, resource)
your_new_after_confirmation_path #your path where you want to land
end
end
you may also need to restart the server.
Source : https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users#redirecting-user

Rails 3 /devise seems to ignore custom controller

Using devise with Rails 3 app, I have read wiki/docs on how to customise the after sign up route, I am using confirmable module, so I think I need to override after_inactive_sign_up_path_for
I think I've done it all right, but it is completely ignoring my custom controller and still routing to the root_path after sign up. Driving me nuts.
My registration is using User model, I have copied the views for devise using the generate task; if i move them to views/registrations devise falls back to the default views (in the gem I guess), so it seems not to be 'noticing' my controller
I have this in my routes:
devise_for :users, :controllers => { :registrations => "registrations" }
match 'sign_up_done' => 'home#sign_up_done', :as => :after_sign_up
Here is my controller: (controllers/registrations_controller.rb)
class RegistrationsController < Devise::RegistrationsController
def after_inactive_sign_up_path_for(resource)
after_sign_up_path
end
def after_sign_up_path_for(resource)
after_sign_up_path
end
end
(Added after_sign_up_path_for just in case, using confirmable)
It just seems to completely ignore my controller, is the naming wrong?
Thanks for any input!
I think your folder structure may have problems. try this structure: ( it's the same as those in Gem folder)
app/controllers/devise/registrations_controller.rb
app/views/devise/registrations/new.html.erb
app/views/devise/registrations/edit.html.erb
and the controller file looks the same as it is declared in the gem folder:
#app/controllers/devise/registrations_controller.rb
# NOT: class RegistrationsController < Devise::RegistrationsController ,
# since you are "overwriting" it.
class Devise::RegistrationsController < DeviseController
def after_inactive_sign_up_path_for(resource)
#...
end
end

Rails 3 override Devise sessions controller

I need to override Devise sessions controller during the login process (Rails 3.0.9, Ruby 1.9.2, Devise 1.3.4), I tried this without any effect
class SessionsController < Devise::SessionsController
# GET /resource/sign_in
def new
resource = build_resource
clean_up_passwords(resource)
respond_with_navigational(resource, stub_options(resource)){ render_with_scope :new }
end
end
Ideas?
EDIT
As indicated in the answer, I also need to change the route. In addition, I also need to copy the views. It's better explained here
http://presentations.royvandewater.com/authentication-with-devise.html#8
My custom strategy:
devise.rb
config.warden do |manager|
manager.strategies.add(:custom_strategy) do
def authenticate!
... authenticate against 3rd party API...
if res.body =~ /success/
u = User.find_or_initialize_by_email(params[:user][:email])
if u.new_record?
u.save
end
success!(u)
end
end
end
Have you altered your route to use your new controller?
/config/routes.rb
devise_for :users, :controllers => {:sessions => "sessions"}

Making changes to the devise user controller in rails?

I have started using devise for my rails app, however i need to make some changes to the controller logic that devise has, my problem is that i assign users roles, and i want to put in params[:user][:role_ids] ||= [] in the update action so if all the roles are unchecked then it actually works.
whats the best way to do this?
class UsersController < Devise::SessionsController
def create
super
end
def update
#edit here
end
end
and make sure you update your routes.rb file:
devise_for :users, :controllers => { :sessions => 'users/sessions' }

Resources