Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-to: Devise after_sign_up_redirect?

I tried to follow the instructions here (GitHub Devise Wiki) but it's not working for me.

I'm trying to get Devise to redirect to /welcome after user signs up. I created a Registrations controller but it's never going into the view. What am I doing wrong here?

Ruby 1.9.2 Rails 3.1.0.rc4 Devise 1.4.2

Thank you

UPDATE:

For some reason after_sign_up_path_for is not triggered but after_sign_in_path_for is triggered. I would still like to get after_sign_up_path_for to work though

_app/controllers/registrations_controller.rb_

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    welcome_path
  end
end

Here's my config/routs.rb

MyApp::Application.routes.draw do
  devise_for :users

  root :to => 'pages#home'

  match "/users", :to => "users#all"
  match "/users/:id", :to => "users#show", :as => :user

  match "/welcome", :to => "users#welcome", :as => :user

  devise_for :users, :controllers => { :registrations => "registrations" } do
    get "/login", :to => "devise/sessions#new"
    get "/register", :to => "devise/registrations#new"
    get "/logout", :to => "devise/sessions#destroy"
    get '/account' => 'devise/registrations#edit'
  end
end

Output from rake routes

new_user_session GET    /users/sign_in(.:format)               {:action=>"new", :controller=>"devise/sessions"}
user_session POST   /users/sign_in(.:format)               {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)              {:action=>"destroy", :controller=>"devise/sessions"}
  user_omniauth_callback        /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"devise/omniauth_callbacks"}
           user_password POST   /users/password(.:format)              {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)          {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format)         {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)              {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)                {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)                       {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)               {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)                  {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)                       {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)                       {:action=>"destroy", :controller=>"devise/registrations"}
                    root        /                                      {:controller=>"pages", :action=>"home"}
                   users        /users(.:format)                       {:controller=>"users", :action=>"all"}
                    user        /users/:id(.:format)                   {:controller=>"users", :action=>"show"}
                    user        /welcome(.:format)                     {:controller=>"users", :action=>"welcome"}
                   login GET    /login(.:format)                       {:controller=>"devise/sessions", :action=>"new"}
                register GET    /register(.:format)                    {:controller=>"devise/registrations", :action=>"new"}
                  logout GET    /logout(.:format)                      {:controller=>"devise/sessions", :action=>"destroy"}
                 account GET    /account(.:format)                     {:controller=>"devise/registrations", :action=>"edit"}
        new_user_session GET    /users/sign_in(.:format)               {:action=>"new", :controller=>"devise/sessions"}
                         POST   /users/sign_in(.:format)               {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)              {:action=>"destroy", :controller=>"devise/sessions"}
  user_omniauth_callback        /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"devise/omniauth_callbacks"}
                         POST   /users/password(.:format)              {:action=>"create", :controller=>"devise/passwords"}
                         GET    /users/password/new(.:format)          {:action=>"new", :controller=>"devise/passwords"}
                         GET    /users/password/edit(.:format)         {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)              {:action=>"update", :controller=>"devise/passwords"}
                         GET    /users/cancel(.:format)                {:action=>"cancel", :controller=>"registrations"}
                         POST   /users(.:format)                       {:action=>"create", :controller=>"registrations"}
                         GET    /users/sign_up(.:format)               {:action=>"new", :controller=>"registrations"}
                         GET    /users/edit(.:format)                  {:action=>"edit", :controller=>"registrations"}
                         PUT    /users(.:format)                       {:action=>"update", :controller=>"registrations"}
                         DELETE /users(.:format)                       {:action=>"destroy", :controller=>"registrations"}
like image 820
Misha M Avatar asked Dec 28 '22 18:12

Misha M


2 Answers

You have devise_for :users twice in your routes.rb. Change the routes.rb as follows:

MyApp::Application.routes.draw do
  devise_for :users, :controllers => { :registrations => "registrations" } do
    get "/login", :to => "devise/sessions#new"
    get "/register", :to => "devise/registrations#new"
    get "/logout", :to => "devise/sessions#destroy"
    get '/account' => 'devise/registrations#edit'
  end

  root :to => 'pages#home'

  match "/users", :to => "users#all"
  match "/users/:id", :to => "users#show", :as => :user

  match "/welcome", :to => "users#welcome", :as => :user

end

If you have activation/deactivation logic, you have to override after_inactive_sign_up_path_for too as mentioned in the devise wiki. Can you tell where it is getting redirected to after sign up?

The reason why after_sign_in_path_for is working may be that you have the same sessions_controller and different registrations_controller, so sign-in works as you expected and the registrations route is getting blocked because you have defined it twice, so all the registration requests and actions are executed in the devise/registrations rather than your custom registrations controller.

So remove that, change the routes.rb as mentioned above, and see what happens.

like image 111
felix Avatar answered Jan 10 '23 21:01

felix


Your devise_for looks kind of odd. The after_sign_up_path_for should go fine as a public method in your application_controller.rb

Change your routes file to just be devise_for :users, as well as placing after_sign_up_path_for in your application_controller.rb

like image 42
Dex Avatar answered Jan 10 '23 20:01

Dex