How-to: Devise after_sign_up_redirect?

南笙酒味 提交于 2019-12-05 14:31:19
felix

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.

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

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.

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!