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?
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'}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With