7

I am using Devise in my rails app. My Users model is registerable, which means that anyone can go to /users/sign_up and create a new account.

Is it possible to protect this route, so that only signed_in users can create new accounts?

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189

2 Answers2

12

Create a Controller with class Devise::RegistrationsController heriting. After you can add your filter. You just need define this controller like registration controller

class RegistrationsController < Devise::RegistrationsController
  before_filter :authenticate_user!
end

In your routes.rb

devise_for :users, :controllers => { :registrations => 'registrations'}
shingara
  • 46,608
  • 11
  • 99
  • 105
  • 4
    @nverinaud I know it's been more than a year since you commented but, just as a heads up, you can solve that by using `authenticate_scope!` instead. – kettlepot Sep 21 '13 at 14:29
4

It didn't worked for me because authenticate_user! is not getting called.

I fixed it that way :

class RegistrationsController < Devise::RegistrationsController
    before_filter :prevent_sign_up

private
    def prevent_sign_up
        redirect_to new_user_session_path and return
    end
end
nverinaud
  • 1,270
  • 14
  • 25
  • 1
    You have a great point here, usually a standard Rails app doesn't use Devise for every action, so your solution is very valid. This is what worked for me. – Puce Mar 02 '14 at 23:54