简体   繁体   中英

Rails Devise adding user when log in

I use Devise gem to log in, registration and others purposes and it's running great. Problem starts, when I log into the application and I want to add another user from inside. I only see "You are already signed in." and nothing else.

So I create another controller for this action- "manager", but nothing changes.

Controller looks like this:

class ManagerController < ApplicationController
  skip_before_filter :require_no_authentication

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    respond_to do |format|
      if @user.create
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
end

And routes:

devise_for :users

resources :manager

How can I have both - registration from outside and adding users from inside?

Rails 4, Devise 3.2.4

Devise::RegistrationsController has require_no_authentication before filter by default.

So it's needed to skip it:

You need to overwrite registration controller. Paste this lines at starting of the controller

skip_before_filter :require_no_authentication
before_filter :authenticate_user!

Because we want to use registration controller after user loges in.

Basically registration comes first in picture but here we need to use it inside, so no need to make more controller and just overwrite devise methods and controller.

-------------------------------------------------------------Updates------------------------------------------------------------

In model just add this method

Model/User.rb before_validation :generate_pwd

def generate_pwd
  your_generated_pwd = @Random password string
  self.password = your_generated_pwd
end

In Route.rb

resources :managers

In views/managers/new.html.erb

<%= form_for User.new do |f| %>
  <%= f.email_field :email %>
  <%= f.submit %>
<% end %>

and use new_manager_path to open this form. Form will use registration_controller's create method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM