How-to: develop after_sign_up_redirect?

I tried following the instructions here ( GitHub Devise Wiki ), but it does not work for me.

I am trying to get Devise to redirect / greet after the user logs in. I created the Registration controller, but it is never included in the view. What am I doing wrong here?

Ruby 1.9.2 Rails 3.1.0.rc4 Develop 1.4.2

thanks

UPDATE:

For some reason, after_sign_up_path_for does not start, but after_sign_in_path_for starts. I would still like to get after_sign_up_path_for though

_app / controllers / registrations_controller.rb _

class RegistrationsController < Devise::RegistrationsController protected def after_sign_up_path_for(resource) welcome_path end end 

Here is 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 

Exit 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"} 
+4
source share
6 answers

You have devise_for :users twice on your routes. rb. Modify route.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 should override after_inactive_sign_up_path_for too, as indicated in the development quiz. Can you tell where it is redirected after registration?

The reason after_sign_in_path_for works might be because you have the same session_controller and the different registrations_controller, so the login works as you expected and the registration route is blocked because you defined it twice, so all requests registration and actions are performed in the development / registration, and not in your user registration controller.

So, delete this, change route.rb as above, and see what happens.

+5
source

Your devise_for looks odd. after_sign_up_path_for should look like a public method in application_controller.rb

Change the route file only to devise_for :users , and also place after_sign_up_path_for in application_controller.rb

+2
source

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

+2
source

In my application, I have an after_sign_in_path_for method in my application_controller.rb file. Try putting it there, reboot the server and see if it matters. In addition, I do not have my method specified in protected , but rather private .

+1
source

If you want to redirect users to the welcome page after registration, it sounds like you just want to redirect them after successfully creating the user. So your user_controller will 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 
0
source

The instructions worked fine for me (new registration controller, change route.rb and copy the registration types to the application / view / registration), but I needed to change my .rb routes a bit so that the registration controllers were matched. Ordering is important because the first suitable route will be used - and you want it to be a new check-in route.

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

devise_for: users

(make sure devise_for: users go after the new route to register)

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

0
source

All Articles