简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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