Misha M
Misha M

Reputation: 11309

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"}

Upvotes: 4

Views: 2620

Answers (6)

felix
felix

Reputation: 11552

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.

Upvotes: 5

Peter Todd
Peter Todd

Reputation: 8651

The instructions worked fine for me (new Registrations controller, modify routes.rb and copy the registrations views to app/view/registrations) but I needed to change my routes.rb slightly so that registrations controllers was picked up. Order is important as the first matching route is going to be used - and you want that to be the new registration route.

devise_for :users, :controllers => { :registrations => "registrations" }

devise_for :users

(make sure devise_for :users comes after the new route for registrations)

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L82

Upvotes: 0

cailinanne
cailinanne

Reputation: 8372

You have duplicate devise_for listings in your routes.rb file. Try removing the first one, the simple devise_for :users, and leaving only the second one.

Upvotes: 2

Dex
Dex

Reputation: 12759

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

Upvotes: 2

Chris Barretto
Chris Barretto

Reputation: 9529

If you want to redirect users to a welcome page after signup, it sounds like you just want to redirect them after a successful user create. So your users_controller would look something like this:

class UsersController < ApplicationController
  def new  
      @user = User.new  
  end  

  def create  
    @user = User.new(params[:user])  
    if @user.save
      flash[:notice] = "Registration successful."  
      redirect_to welcome_path  
    else  
      render :action => 'new'  
    end  
  end

  def show
    @user = User.find(params[:id])
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:notice] = "Successfully updated user."
      redirect_to @user
    else
      render :action => 'edit'
    end
  end
end

Upvotes: 0

Kyle Decot
Kyle Decot

Reputation: 20835

In my application I have the after_sign_in_path_for method in my application_controller.rb file. Try putting it in there, restarting your server and see if that makes any difference. Also, I don't have my method listed under protected but rather private.

Upvotes: 1

Related Questions