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

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

问题描述

我在我的 Rails 应用中使用 Devise.我的用户模型是可注册的,这意味着任何人都可以转到 /users/sign_up 并创建一个新帐户.

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?

推荐答案

创建一个继承类 Devise::RegistrationsController 的控制器.在您可以添加过滤器之后.你只需要像注册控制器一样定义这个控制器

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

在你的 routes.rb 中

In your routes.rb

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

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

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