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

后端 未结 8 2092
独厮守ぢ
独厮守ぢ 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:42

    in case someone still looking for help, because it tooks a while for this to work , no clear answers

    in your controller

     class UsersController < ApplicationController
    
      def new
        @user = User.new
      end
    
      def add_user
        @user = User.new(user_params)
         if @user.save!
           redirect_to root_path
         end
      end
    
      private
    
    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end
    end
    

    in your routes :

    get  'employees',  to: 'users#new'
    post 'employees',  to: 'users#add_user'
    

    and finally a form like this:

    <%= form_for User.new , :url => {:action => "add_user"} do |user| %>
      <%=user.email_field :email%>
      <%=user.password_field :password%>
      <%=user.password_field :password_confirmation%>
    
      <%=user.submit 'add'%>
    <%end%>
    

提交回复
热议问题