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?

8 Answers 8

15

Found.

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

6
  • Same stinkin' problem I had! Took me over an hour to figure it out (by finding this answer). Thanks!
    – MattSlay
    May 2, 2012 at 19:16
  • @KleberS. Where is that specifically? i.e. did you edit a certain file..?
    – kingsfoil
    Jul 18, 2014 at 23:20
  • Nevermind. users.rb (model) In case anyone else is wondering.
    – kingsfoil
    Jul 18, 2014 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, 2015 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? Jul 5, 2016 at 17:56
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
5
  • 1
    You mean, create a form_tag instead the form_for?
    – Kleber S.
    Aug 16, 2011 at 9:22
  • You can use whatever you want.
    – Dex
    Aug 16, 2011 at 9:25
  • It is better to do this with a separate Admin model using devise_for :admin
    – Dex
    Aug 16, 2011 at 9:32
  • Going this way, if I need to have a managerrole, should I create another model to allow it to create new users too? it doesn't make much sense..
    – Kleber S.
    Aug 16, 2011 at 9:36
  • @KleberS. Sorry, I meant Admin controller and namespace.
    – Dex
    Jun 17, 2014 at 22:57
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
2
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.

2
  • 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 2, 2015 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, 2019 at 21:06
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.

0
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.

1
  • 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, 2011 at 9: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%>
1
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" } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.