[英]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.