2014-02-19 60 views
1

如果帳戶未啓用,我試圖自動註銷用戶。如果帳戶未啓用,則自動註銷用戶(設計)

<% if current_user %> 
    <% if current_user.enabled == "no" %> 
    <% flash.alert = "Your account is disabled! Please contact support for more information" %> 
    <% sign_out current_user %> 
    <% end %> 
<% end %> 

但我得到一個 「爲#<#未定義的方法`SIGN_OUT」:0x007f92001bce68>」 的錯誤。我正在使用設計。有沒有辦法做到這一點?

+0

你應該做的控制器。 –

+0

@MarekLipka我會在控制器中「做它」,使其工作後。先讓它工作,然後重構。 –

+2

你是對的紅色,綠色,重構,但你應該仍然保持MVC架構。 – dennis

回答

2

首先設置你的enabled屬性爲布爾類型的更新。

在你的ApplicationController

before_filter :check_user 

def check_user 
    sign_out current_user if current_user && current_user.disabled? 
end 

在用戶模式:

def disabled? 
    enabled == false 
end 
1

有一個在設計一種方法使用該名稱,並根據您的要求所有的

def after_sign_in_path_for(resource_or_scope) 
     super 
     session[:id] = current_user.id 
     session[:enable] = current_user.email 
     if current_user.enabled = "no" 
      flash.alert = "Your account is disabled! Please contact support for more information"   
      sign_out_path 
     else 
     flash.alert = "Successfully login." 
     redirect_to "specify your path for redirect" 
     end 
    end 
+0

謝謝,但不完全是我在找什麼。我想立即註冊用戶,而不是在sign_in之後。 –

+0

我之後是這樣的:http://stackoverflow.com/q/6315624,但它不適用於Rails4(?)或我的情況...正如我試過。 –

+0

好的,然後在過濾器檢查用戶是否啓用之前編寫一個方法。 –