Devise redirect on sign up - ruby-on-rails

I know this question was asked several times. I tried them but none of them solved my problem. I used devise for users. I want to redirect the user to a different page on sign up rather than signing in directly. I created registration controller, and tried overriding "after_inactive_sign_up_path_for" but it didn't work out. I 'm using devise confirmable also. So until the user is verified I should redirect him to other page.
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
"http://www.google.com" # Or :prefix_to_your_route
end
end
routes.rb
devise_for :users, controllers: { registrations: "registrations" }
Next I tried to move the registrations into a different folder in the controllers folder. But that too didn't work out.
FYI: I'm using rails 5 and devise 4.2.1.

Your controller should be in the path app/controllers/users/registrations_controllers.rb.
Then your routes should be
devise_for :users, controllers: { registrations: "users/registrations" }

Related

Devise before_action :authenticate_admin! not authenticating admin user

I have used devise numerous times but currently facing an issue at the moment.
I used this devise wiki to set up devise with multiple user models which i have done multiple times. https://github.com/heartcombo/devise/wiki/How-to-Setup-Multiple-Devise-User-Models
In my admins_controller.rb i have the following code
class AdminsController < ApplicationController
before_action :authenticate_admin!
end
My routes.rb
Rails.application.routes.draw do
devise_for :admins, path: 'admins', controllers: {sessions: "admins/sessions", registrations: "admins/registrations"}
namespace :admins do
root "dashboards#index"
end
end
Everything else works well but after i try to sign in as an admin, it should redirect to my admins root but i always get this error
You need to sign in or sign up before continuing.
But when i do admin_signed_in? or current_admin. I get true and my admin record accordingly. Which means the admin is already signed in.
When i comment the before_action code, then it works perfectly.
Currently stuck and cannot think of why and how to solve this issue.

Devise Invitable controller seems not to be reached?

I installed devise-invitable and using the standard devise-invitable controller (which I didn't generate) it functions properly. However, when I tried generating a custom controller for devise-invitable, I encountered some issues. Please find below the steps I took.
Steps I took to generate the controller
Following the documentation of devise I tried generating the invitations controller via the console which failed:
rails generate devise:controllers users -c=invitations
Running via Spring preloader in process 64830
Could not find "invitations_controller.rb" in any of your source paths. Your current source paths are:
/Users/name/code/name/app/lib/templates/devise/controllers
/Users/name/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/devise-4.7.0/lib/generators/templates/controllers
When this didn't work, I manually tried implementing:
a controller in the folder where all other devise controllers were generated
Adding the invitations controller to routes.rb
=> This didn't seem to work either, because I tried breaking the controller to see if it was reached, but it doesn't break.
controllers/users/invitations_controller.rb
class Users::InvitationsController < Devise::InvitationsController
def new
#hotel = Hotel.find(params[:park_id])
#user = User.new
#user.hotel = #hotel
text to cause an error message
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {
sessions: 'users/invitations'
}
The problem is in your routes since an invitation is not an session.
If you changes sessions to invitations in your routes then it will hit the users/invitations controller.
# routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {
invitations: 'users/invitations'
}
end

Session Value not set in ror devise form

i had three ror devise forms...so i thought of adding after_sign_up_path_for .its working perfectly but the problem i am having is one of the forms using session hascode..when its redirected it shows hashcode is not set...
i tried lot of thins but its working when i add devise_for :users alone in routes.rb the session code is set but if i add this line ,session is not set but all three forms are redirecting correctly..
devise_for :users, :controllers => {:registrations => "registrations"}
i am not sure what is causing the problem..please anybody help...
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
if resource.planner_type == "planner"
'/path1/invited_new'
elsif resource.planner_type == "bride"
'/path2/invited_new'
else
'/path3/invited_new'
end
end
end

Override Devise Registrations Controller Two Times?

I have two models, User and Admin. I want to know if its possible to override Devise's Registrations Controller and have two custom Registrations Controllers - one for each model.
I know its possible to get what I want by overriding the registrations controller and I would just use If else statements although (correct me if I am wrong) I believe its better to avoid having many if else statements if possible.
You can see what I've done so far on another post - I have scoped views and it uses the wrong set of views for some reason. Devise Views with Multiple Models
Yes it's possible
Routes
When you generate Devise for both models, you'll have to add it to your routes:
#config/routes.rb
devise_for :users
devise_for :admin
Devise actually uses these routes to populate its arguments, one of which is the controllers argument:
#config/routes.rb
devise_for :users, controllers: { sessions: "sessions", registrations: "registrations" }
devise_for :admin, controllers: { registrations: "admin/session" }
Controllers
This will allow you to create controllers to override the Devise default ones:
#app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
#Your Code Here
end
#app/controllers/admin/registrations_controller.rb
class Admin::RegistrationsController < Devise::RegistrationsController
#Your Code Here
end

Setup devise with custom registration controller

I'm working on a rails site using devise, where we do not want user sign ups just yet. User authentication is so we can login to access restricted parts of the site and add/edit things as we see fit. So for now, I created the following controller:
class Users::RegistrationController < Devise::SessionsController
def new
end
end
And setup my routes in this fashion:
devise_for :users, :controllers => { :registration => "users/registration" }
However, when I run rake routes, I still see a returned value for the create action on the registration controller. Any ideas on how to get rid of it?
Try using :registrations instead of :registration. Also, it seems like your custom controller class should be defined via:
class Users::RegistrationsController < Devise::RegistrationsController

Resources