开发者

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

I'm going to create a multi user ap开发者_JAVA技巧p, 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?


Found.

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


In a controller method can't you just go:

def create_user
    @user = User.new(:email => params[:email], :password => params[:password])
    @user.save
    ...
end


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


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.


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.


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.


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%>


@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" } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜