Pradeep Sapkota
Pradeep Sapkota

Reputation: 2072

How to restrict registration and create own sign up in devise?

In Devise we can restrict user to sign up directly by removing :registerable symbol form User model. But my theme is to create own user sign up page and controller and allow admin to create users manually. I tried many times but the problem is that how to encrypt password so that it becomes accessible by devise during login. If there is any idea please help.

Upvotes: 1

Views: 869

Answers (2)

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2072

I did this for solving problem

  1. I just deleted the :registerable form user model
  2. Then manually create a user controller and form for creating data.
  3. Then set necessary validation in user model.
  4. When submitting data the user model automatically encrypted and saved password.

And thank you for effort for helping.

Upvotes: 2

Emu
Emu

Reputation: 5905

Here are some suggestions and solution:

  1. Don't delete the :regesterable from User model. Instead, just define a different route for the registration in routes.rb file, so that others cannot find that path to register. e.g. devise_for :users, path_names: { sign_up: 'register_not_allowed' }

  2. Create a module like Admin and then define a controller like UsersController under this module. e.g.

    module Admin class UsersController < ApplicationController end end

  3. In the controller create new, edit, show, create, update methods as you do for any controller. create a form having fields with email, password, password_confirmation and also with the additional fields which is an attribute in User model. Then require them in the strong parameters of your admin/users controller. Now when you create an user using this form no encryption of password is needed.

app/views/admin/users/_form.html.erb

<%= form_for [:admin, @user] do |f| %> <% end %>

Upvotes: 3

Related Questions