15

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?

Kleber S.
  • 8,110
  • 6
  • 43
  • 69

8 Answers8

15

Found.

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

Kleber S.
  • 8,110
  • 6
  • 43
  • 69
  • Same stinkin' problem I had! Took me over an hour to figure it out (by finding this answer). Thanks! – MattSlay May 02 '12 at 19:16
  • @KleberS. Where is that specifically? i.e. did you edit a certain file..? – kingsfoil Jul 18 '14 at 23:20
  • Nevermind. users.rb (model) In case anyone else is wondering. – kingsfoil Jul 18 '14 at 23:25
  • 2
    When I removed ":registerable", it made me unable to sign in at all (presumably because it's no longer 'registerable')…anyone else have this? (Using Rails 4, btw) – Jo.P Jun 11 '15 at 13:21
  • does that mean after we remove registerable module we need to create new controller, methods and views ourselves or we can use devise's existing controller, methods and views? – Tejas Patel Jul 05 '16 at 17:56
  • @TejasPatel yes :( If you need registrations, it is better to rename action of /users/create to /users/create_something, as you see in the second answer – Gediminas Šukys Dec 24 '16 at 10:48
8

In a controller method can't you just go:

def create_user
    @user = User.new(:email => params[:email], :password => params[:password])
    @user.save
    ...
end
Dex
  • 12,527
  • 15
  • 69
  • 90
3

There is another solution.

You must override registration controller and remove action (or actions) from prepend_before_filter.

DeviseRegistrationController source is here.

You can see:

prepend_before_filter :require_no_authentication, only: [:new, :create, :cancel]

It jumps into require_no_authentication before create method. If you want create new user while you are logged in, just remove :create from array.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
user1263663
  • 75
  • 1
  • 6
  • 1
    Worked for me :) Thanks for that. One thing I'm wondering though, is if you have a solution for protecting "/users/sign_up" from being accessible to everyone – mhz Sep 02 '15 at 15:19
  • I had to modify the relevant line to `prepend_before_action :require_no_authentication, only: [:cancel]` maybe a newer version of devise. – khaverim Feb 17 '19 at 21:06
3

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
Mourkeer
  • 111
  • 3
  • 8
3

I added in the Registrations Controller:

class RegistrationsController < Devise::RegistrationsController
    ...
    skip_before_action :require_no_authentication, only: [:new, :create]
    ...
end

And it worked for me. I can now go and create a new user.

2

You could either override the default devise controller and add in your custom logic, or, It would probably be easier however to make a new (Admin) controller and simply create a user using one of it's actions.

@user = User.create!(:name => params[:foo], :email => params[:bar])
redirect_to @user

Devise has loads of guides on how to customise it's behaviour here: https://github.com/plataformatec/devise/wiki/_pages

This one in particular may be of interest to you: https://github.com/plataformatec/devise/wiki/How-To:-Manage-Users-with-an-Admin-Role-(CanCan-method) But make sure to have a look over the rest of the articles, there's a lot of them.

Blake Simpson
  • 1,078
  • 6
  • 10
  • Thank you for the link. I have saw this before and that doesn't seem to have anything "special" at least on the `create` method. – Kleber S. Aug 16 '11 at 09:43
1

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%>
Bechir Segni
  • 71
  • 1
  • 8
  • When giving an answer it is preferable to give [some explanation as to WHY your answer](http://stackoverflow.com/help/how-to-answer) is the one. – Stephen Rauch Feb 12 '17 at 00:30
0

@Mourkeer +1
For simple_form 4.2.0 take @Mourkeer code and replace route.rb by:

# route.rb
devise_for :users, path_names: { registration: "registrations" } 
Thooams
  • 21
  • 4