过滤器之前的设计,阻止访问“new_user_registration_path”除非用户已登录 [英] Devise before filter that prevents access to "new_user_registration_path" unless user is signed-in

查看:97
本文介绍了过滤器之前的设计,阻止访问“new_user_registration_path”除非用户已登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的rails应用程序中使用Devise。我的用户模型是可注册的,这意味着任何人都可以访问 / users / sign_up 并创建一个新帐户。



是否可以保护这个路由,所以只有signed_in的用户可以创建新的帐户?

解决方案

创建一个具有类Devise :: RegistrationsController heriting的Controller。添加过滤器之后你只需要定义这个控制器,如注册控制器

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

在您的路线。

  devise_for:users,:controllers => {:registrations => 'registration'} 


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'}

这篇关于过滤器之前的设计,阻止访问“new_user_registration_path”除非用户已登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆