Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 trying to create a new user, being already signed in, I'm getting redirect to root_path with an error message that says "You are already signed in".

So, what should I do to make this possible?

like image 736
Kleber S. Avatar asked Aug 16 '11 09:08

Kleber S.


3 Answers

Found.

I have just to removed the registerable module from devise and it works.

like image 109
Kleber S. Avatar answered Oct 03 '22 17:10

Kleber S.


In a controller method can't you just go:

def create_user
    @user = User.new(:email => params[:email], :password => params[:password])
    @user.save
    ...
end
like image 32
Dex Avatar answered Oct 03 '22 17:10

Dex


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
like image 20
Mourkeer Avatar answered Oct 03 '22 19:10

Mourkeer