gpt4 book ai didi

ruby-on-rails - Rails Devise,如何只为 super 用户制作 "sign up"

转载 作者:行者123 更新时间:2023-12-04 07:39:15 27 4
gpt4 key购买 nike

我正在使用 Devise for Rails 3.2.1 应用程序。我想阻止注册,以便只有 super 用户/管理员才能添加新用户。我该如何实现?

(更新)我尝试了 Devise before filter that prevents access to "new_user_registration_path" unless user is signed-in 中建议的技巧

但它不起作用,因为添加的 Controller 显然没有密码保护新路由“/users/registrations”:

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

最佳答案

我建议您创建一个仅供 super 用户使用的 CRUD,他将能够创建用户,而不是将对注册路径的访问限制为仅 super 用户。您可以在这里找到帮助:https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface .

如果您想使用注册,我会做几乎与您在回答中所做的相同的事情,但 before_filter 应该是这样的:

before_filter :authenticate_user!, :redirect_unless_admin

# (...)

private
def redirect_unless_admin
unless current_user.admin
redirect_to root_path
end
end

关于ruby-on-rails - Rails Devise,如何只为 super 用户制作 "sign up",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9457051/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com