Devise: How to create a new user being already logged in?

后端 未结 8 2089
独厮守ぢ
独厮守ぢ 2021-02-02 16:22

I\'m going to create a multi user app, so, I will have a admin user that will have permission to create new ones.

I\'ve created the UsersControllerbut when

8条回答
  •  别跟我提以往
    2021-02-02 16:25

    This is how I am doing it in 2015

    # in your terminal
    rails g controller Registrations
    

    Registrations controller should look like this,

    # registrations_controller.rb
    class RegistrationsController < Devise::RegistrationsController
    
      skip_before_filter :require_no_authentication, only: [:new]
    
      def new
        super
      end
    
    end
    

    The important line is the skip_before_filter... That will disable the requirement that there be no user logged in.

    The routes for the controller looks like this,

    # routes.rb
    devise_for :users,
        controllers: {:registrations => "registrations"}
    

    That will tell devise to use your custom registrations controller

    Finally, setting up a custom route to that action:

    # routes.rb
    as :user do
      get "/register", to: "registrations#new", as: "register"
    end
    

提交回复
热议问题