繁体   English   中英

从头关闭Rails应用程序上的用户注册

[英]Turning off User registration on rails app from scratch

我有一个非常简单的Rails应用程序,我从头开始创建了一个非常基本的身份验证来处理用户帐户等,我想暂时关闭我的应用程序中的注册,并想知道这样做的最佳方法是什么。 这是我的创建用户操作:

def create
  @user = User.new(params[:user])
  if @user.save
    session[:user_id] = @user.id
    redirect_to root_url, notice: "All signed up!"
  else
    render "new"
  end
end

我想这是确保用户无法注册的最佳方法是检查环境变量,然后在控制器中再次检查此变量,如下所示:

def create
  @user = User.new(params[:user])
  if @user.save && registration == 1
    session[:user_id] = @user.id
    redirect_to root_url, notice: "All signed up!"
  elsif registration == 0
    redirect_to root_url, notice: "Sorry we are not accepting new signups"
  else
    render "new"
  end
end

这是最好的方法吗? 在记录保存中,我正在检查注册变量是否等于true,然后保存,如果不是,则返回false,然后通过消息重定向。

更好的方法是在新方法中放置重定向,并向用户重定向回有关已关闭注册的消息。 您当前的解决方案是阴险的,并且在用户已经花了很多时间注册表格后发出通知。

所以会像这样:

def new
 if true #switch to false if you need to enable registration.
  redirect_to root_url, notice: "Sorry we are not accepting new signups"
 else
  #Your other code goes here
 end
end

在这种情况下,您不会强迫用户填写表格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM