Devise before filter that prevents access to "new_user_registration_path" unless user is signed-in
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
精彩评论