2013-05-15 53 views
0

我有一個非常簡單的Rails應用程序,我從頭開始創建一個非常基本的身份驗證來處理用戶帳戶等,我想暫時關閉我的應用程序中的註冊並想知道什麼最好的方法是做到這一點。這是我創建的用戶動作:從頭開始關閉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然後重定向與消息。

回答

1

更好的辦法是放置在新方法裏面,將消息重定向回用戶關於註冊的關閉。您目前的解決方案是陰險的,並在用戶已經花時間登記表格後發出通知。

因此,這將是這樣的:

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 

在這種情況下,你不能強迫用戶填寫表單。

+0

謝謝,公平點提高了用戶體驗。 – calabi

相關問題