Redirect User to Signup

前端 未结 5 983
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 20:47

Using before_action :authenticate_user! to check if user logged in. But it sends users to login instead of signup.

Tried different

5条回答
  •  旧时难觅i
    2021-02-05 21:19

    the way I like to set up my users area using devise is:

      # config/routes.rb
      devise_for :users, :controllers => {
        registrations: 'users/registrations',
        sessions: "users/sessions",
        passwords: 'users/passwords',
        confirmations: 'users/confirmations'
      }
    
      authenticate :provider do
        namespace :providers do
          ....
        end
      end
    

    Then I have a controller that manages all other users controllers like this

    #app/controllers/user_controller.rb
    class UserController < ApplicationController
      before_filter :authenticate_user!
      layout 'users/default'
      before_filter :check_user_active
    
      private
      def check_user_active
        unless current_user.active
          flash[:notice]= t(:user_not_active)
          sign_out current_user
          redirect_to new_user_session_path
        end
      end
    end
    

    all my other user controllers look inherit from it like

    #app/controllers/users/users_controller.rb
    class Users::UsersController < UserController
      ...
    end
    

    I hope that this helps.

提交回复
热议问题