简体   繁体   English

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

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

I have a very simple Rails app which I created a very basic authentication from scratch to handle user accounts etc, I want to turn off registration in my app for the time being and wondering what the best way to do this would be. 我有一个非常简单的Rails应用程序,我从头开始创建了一个非常基本的身份验证来处理用户帐户等,我想暂时关闭我的应用程序中的注册,并想知道这样做的最佳方法是什么。 Here is my create user action: 这是我的创建用户操作:

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

I was thinking would be best way to ensure a user couldn't signup be to check an environment variable and then double check this in my controller something like the following: 我想这是确保用户无法注册的最佳方法是检查环境变量,然后在控制器中再次检查此变量,如下所示:

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

Would this be the best approach? 这是最好的方法吗? On the record save I'm checking if the registration variable equals true and then save if not does it equal false then redirect with a message. 在记录保存中,我正在检查注册变量是否等于true,然后保存,如果不是,则返回false,然后通过消息重定向。

Better way is to place inside new method redirect back with message to user about closed registration. 更好的方法是在新方法中放置重定向,并向用户重定向回有关已关闭注册的消息。 Your current solution is insidious and throw notice after user already spent time on registration form. 您当前的解决方案是阴险的,并且在用户已经花了很多时间注册表格后发出通知。

So it would be like this: 所以会像这样:

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

In this case you not forcing users to fill form. 在这种情况下,您不会强迫用户填写表格。

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

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