gpt4 book ai didi

ruby-on-rails - 暂时禁用设计中的注册

转载 作者:行者123 更新时间:2023-12-04 06:15:11 24 4
gpt4 key购买 nike

我在 Rails 5 项目中使用 Devise,我想暂时禁用新用户注册。我知道我可以删除 View 的链接,或编辑路由文件来完成此操作,但这需要使用新部署更改代码。我想在我的管理控制台中添加一个选项,以禁用新用户注册一段时间的功能。

我启用了此功能,但它是一个hacky 解决方法。当我禁用人们注册的功能时,我目前所做的就是在设计 View 中隐藏“新用户注册”链接。任何知道默认路径的人都可以解决这个问题。

如何在运行时更改此功能?

最佳答案

关注 This用于覆盖设计的注册 Controller 的链接。
基本上,您需要覆盖设计默认注册 Controller 并为 new & create 设置一个前置过滤器检查管理员是否允许创建新用户的操作,如果不允许,则重定向到 root_path或任何你想要的路径。

更改 config/routes.rb 中的 devise_for 行以覆盖注册 Controller :

devise_for :users, controllers: { registrations: "registrations"}

应用程序/ Controller /registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController

before_action :check_new_registration_allowed?, only: [:new, :create]

protected

def check_new_registration_allowed?
redirect_to root_path unless @@allow_new_registration
end

end

请注意,我使用了 @@allow_new_registration用于检查是否允许新注册的全局变量。您可以在管理员操作后打开/关闭此功能。或者,如果您将这些信息存储在数据库中并进行查询,那就太好了。

关于ruby-on-rails - 暂时禁用设计中的注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38415483/

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