How to Remove/Disable Sign Up From Devise
Solution to removing sign_up path from Devise
Enter the following at the beginning of routes.rb
Rails.application.routes.draw do
devise_scope :user do
get "/sign_in" => "devise/sessions#new" # custom path to login/sign_in
get "/sign_up" => "devise/registrations#new", as: "new_user_registration" # custom path to sign_up/registration
end
...After the statement above, add the following below in routes.rb
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
end
This will remove/disable the user/sign_up
path for Devise without breaking edit_user_registration_path
Restart your rails server and it should work.
How do I remove the Devise route to sign up?
I tried to do this as well, but a thread on the devise google group dissuaded me from searching for a really clean solution.
I'll quote José Valim (the Devise maintainer) :
There isn't a straight-forward option. You can either provide a patch
or use :skip => :registerable and add only the routes you want.
The original question was :
Is there any good way to remove a specific route (the delete route)
from Rails?
Temporary disabling of registrations in Devise
Follow This Link for overriding the Registration Controllers for Devise.
Basically you need to override Devise default Registeration controller and put up a before filter for new & create
action which checks if admin allows to create new users if not then redirect to root_path
or whatever path you want.
Alter the devise_for line in config/routes.rb to override the registration controller:
devise_for :users, controllers: { registrations: "registrations"}
app/controllers/registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController
before_action :check_new_registration_allowed?, only: [:new, :create]
protected
def check_new_registration_allowed?
redirect_to root_path unless @@allow_new_registration
end
end
Please be noted that I used @@allow_new_registration
global variable to check if new registration is allowed. You can turn this on/off after admin action. Alternatively it would be good if you store these information in DB and query it.
how to disable automatic sign-in after sign-up in Devise
There is no point in using the Devise sign up functionality in this case. Devise registrations are a ready-made solution for providing end user sign up which is very different from what you want.
Instead the logical solution would be to just use a plain controller action.
# config/routes.rb
resources :users
class UsersController < ApplicationController
before_action :authenticate_user!
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user, notice: 'User created successfully.'
else
render :new
end
end
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
Admins would create users via /users/new
.
added
The route created by devise_for :users
has a higher priority than the one that leads to UsersController#create
.
user_registration POST /users(.:format) devise/registrations#create
This is because routes have priority in the order they are defined.
A basic fix would be:
Rails.application.routes.draw do
# Recreatetes the Devise registrations routes
# They act on a singular user (the signed in user)
# "as: :user_registration" gives us the same named routes as devise_for
resource :users,
only: [:edit, :update, :delete],
controller: 'devise/registrations',
as: :user_registration do
get 'cancel'
end
devise_for :users, skip: [:registrations]
resources :users # creates the "normal" CRUD routes for users
end
skip: registration vs remove :registerable
If you are not interested in user registration, both options give you the same result, although I prefer to remove the registerable
module from the User model to avoid loading it and not use it at all.
You can see the devise
method in the next link, so you can understand what you're avoiding by not loading the module:
https://github.com/plataformatec/devise/blob/master/lib/devise/models.rb#L77
On the other hand, if you are interested in user registration through any subclass (say buyer or seller, using STI) not the class itself (user), you need the registerable
module on User model and something like that on routes:
devise_for :users, skip: :registrations
devise_for :buyers, only: :registrations
devise_for :sellers, only: :registrations
I hope it helps.
disabling Devise registration for production environment only
Since others are having the problem I'm having (see my comments). Here is exactly how I fixed it. I used murphyslaw's idea. But you also need to make sure devise uses your new controller for the registration routing, or it won't do much for you.
Here is my controller override:
class RegistrationsController < Devise::RegistrationsController
def new
flash[:info] = 'Registrations are not open yet, but please check back soon'
redirect_to root_path
end
def create
flash[:info] = 'Registrations are not open yet, but please check back soon'
redirect_to root_path
end
end
I've added flash messages to inform anyone who somehow stumbles upon the registration page why it isn't working.
Here is what is in my routes.rb
if Rails.env.production?
devise_for :users, :controllers => { :registrations => "registrations" }
else
devise_for :users
end
The controllers hash specifies that I want it to use my overridden registrations controller.
Anyways, I hope that saves someone some time.
Related Topics
Sending a Post Request with Net/Http
How to Do Basic Authentication Over Https in Ruby
Cucumber, Capybara and Selenium - Submitting a Form Without a Button
Errno::Enoent: No Such File or Directory Ruby
Carrierwave: Create the Same, Unique Filename for All Versioned Files
How to Get the Ruby Documentation from the Command Line
How to Test a Ruby Application Which Uses Mechanize
Rails Scope Find with Current User
What Are the Uppercase and Lowercase Rules of Ruby Method Name
Rails Plugin for API Key + Secret Key Signing
How to Pass Arguments from the Parent Task to the Child Task in Rake
Configuring Jekyll for Github Project Pages