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
Related
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
How do I use the Ruby on Rails devise gem to stay on the Edit User page after I edit my information?
I figured out how to do it.
First, create a RegistrationsController in the CLI by typing
rails g controller Registrations
Then, create a new folder called my_devise in your app/controllers folder, and move the newly generated RegistrationsController into that folder.
Next, open up RegistrationsController, and change the contents to
class MyDevise::RegistrationsController < Devise::RegistrationsController
protected
def after_update_path_for(resource)
user_path(resource)
end
end
Now, move the app/views/devise/registrations folder to a new folder called app/views/my_devise/registrations, but leave the sessions and shared folders in the devise folder.
Then, in the config/routes.rb file, change the line devise_for :users to devise_for :users, :controllers => {:registrations => "my_devise/registrations"}.
And that's it! I think I covered it all.
Override the after_update_path_for(resource) method. In your application controller:
def after_update_path_for(resource)
user_edit_path(resource)
end
I am using Devise 1.5.3.
I inherited Devise::RegistrationsController for customizing my own path for after_update:
class RegistrationsController < Devise::RegistrationsController
protected
def after_update_path_for(resource)
some_path
end
end
After that Rails tries to find views for registration actions (new, create, etc) in views/registrations/ but no in /views/devise/registrations
Sure, I can copy all from /views/devise/registrations to views/registrations/. But it's not suitable for me, because rest of my view (for not customized controllers) still are in /views/devise/registrations.
How Can I fix it?
I think it should work if you scope your RegistrationsController into Devise's scope:
class Devise::MyRegistrationsController < Devise::RegistrationsController
# ...
end
Of course you can't use the same name for the RegistrationsController and you have rename the views/devise/registrations folder to views/devise/my_registrations and you have to update your routes.rb file as well... but with this setup it should work...
Another option, I think, would be to overwrite/inject the method in an initializer:
class Devise::RegistrationsController
protected
def after_update_path_for(resource)
some_path
end
end
which may be simpler...
Building on Vapire's answer. I think the inject option is simplest/best.
Because I want to include a permanent 'Edit my Registration' in the navbar, so that it can be called from any page,I also want to send the user back to the page they clicked from.
I found a simple way to do this was to override some of the controller behaviour and use a session variable to capture the referring path. Using a session variable keeps the referrer path intact if the form has to be reloaded to correct errors.
routes.rb
devise_for :users, :controllers => {:registrations=>'users/registrations'}
controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def edit
session[:called_from]=request.referer
super #revert to standard behaviour
end
def after_update_path_for(resource)
session[:called_from] #returns to the page called from
end
end
The accepted answer by #Vapire does not work for me for Rails 4. I had to override the Devise Passwords controller like this:
# users/passwords_controller.rb
class Users::PasswordsController < Devise::PasswordsController
protected
def after_resetting_password_path_for(resource)
signed_in_root_path(resource)
end
end
There is documentation on this method here: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in
How do I tell Devise to route the user to a one-time welcome screen when he has just registered?
Make a new controller "RegistrationsController" and customize the appropriate method:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
some_special_page
end
end
If the account that is registered is not active yet, you have to override after_inactive_sign_up_path_for method.
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
some_special_page
end
end
Modify config/routes.rb to use the new controller
devise_for :users, :controllers => { :registrations => "registrations" }
Source: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-after-registration-(sign-up)
In the Application controller, add this:
def after_sign_up_path_for(resource)
some_special_page
end
For most of Devise-related questions, hit up their Github wiki
by default Devise will forward user to: user_root (if your model name is user)
so you can define named route
get "/welcome" => "welcomes#index", :as => "user_root"
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' }