Reputation: 303
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.
Upvotes: 0
Views: 64
Reputation: 770
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.
Upvotes: 1