2012-03-22 13 views
0

我在試圖從設計中獲取current_user時遇到了實際問題,從此處確定帳戶ID,然後將此作爲變量從此gem傳遞給set_current_tenant_to方法:https://github.com/ErwinM/acts_as_tenant在application_controller中設置類和方法變量

在我的應用程序控制器我有:

class ApplicationController < ActionController::Base 

    protect_from_forgery # See ActionController::RequestForgeryProtection for details  
    helper :all # include all helpers, all the time 

    def get_current_account_id 
    current_account_user = current_user 
    current_account_id = current_account_user.account_id 
    current_account_id 
    end 

    current_account = Account.find(get_current_account_id) 
    set_current_tenant_to(current_account) 

我知道get_current_account_id推導出正確的ACCOUNT_ID,因爲當我把它放在一個電話的before_filter我可以在日誌中看到它輸出正確的數字。但是,當我運行此我得到以下錯誤:

Routing Error 

undefined local variable or method `get_current_account_id' for ApplicationController:Class 

任何意見或指示我如何能得到這個工作將不勝感激。

回答

0

我猜測它是調用一個類級別的方法,而不是一個實例方法,所以你需要調用它自己。同時,你真的不需要所有這些額外的變量:

class ApplicationController < ActionController::Base 
    def self.get_current_account_id 
    current_user.account_id 
    end 
end 
+0

感謝您的嘗試,但它不工作。 有了這個在我的控制器: '高清self.get_current_account_id current_user.account_id 結束 current_account = Account.find(get_current_account_id) set_current_tenant_to(current_account)' 我現在得到了如下回應: '路由錯誤 ApplicationController:Class'的未定義局部變量或方法'current_user':類' – Betjamin 2012-03-22 14:51:55

+1

混淆來自類和實例方法的衝突...乍一看,租戶文檔看起來很有趣,因爲它們看起來像是在課堂設置中運行,但那會鎖定t他認爲第一次運行,而不是運行它的每一個請求...我建議澄清與該作者的那個寶石... – DGM 2012-03-22 15:00:45

0

我猜測你想要做的是創造一個過濾設置當前租戶。你應該這樣做:

class ApplicationController < ActionController::Base 
    before_filter :set_tenant_to_current_account 
    def set_tenant_to_current_account 
    current_account = Account.find(current_user.account_id) 
    set_current_tenant_to(current_account) 
    end 

    def set_current_tenant_to(account) 
    # Your business logic here. 
    @current_tenant = account 
    end 
end 

編輯:把一些存根方法的代碼來表示set_tenant_to

+0

是的,這就是我想,但是當我運行這個我得到: 'NoMethodError in FooController#index 未定義的方法'set_current_tenant_to '爲# 應用程序/控制器/ application_controller.rb:36:在' set_tenant_to_current_account'' – Betjamin 2012-03-22 23:57:14

+0

好吧,這是因爲你沒有在你的控制器的任何地方定義這個方法。你在哪裏定義「set_current_tenant_to」方法? – rscarvalho 2012-03-23 11:29:32

+0

它被設置爲這個寶石的一部分在這裏: https://github.com/ErwinM/acts_as_tenant/blob/master/lib/acts_as_tenant/controller_extensions.rb 任何幫助,將不勝感激。 – Betjamin 2012-03-23 11:34:20

0

當你定義的應用程序控制器的任何自定義的方法,那麼你必須在使用該叫它

呼叫。

class ApplicationController < ActionController::Base 

    before_filter :get_current_account_id 


    protect_from_forgery # See ActionController::RequestForgeryProtection  
    helper :all # include all helpers, all the time 

    def get_current_account_id 
    current_account_user = current_user 
    current_account_id = current_account_user.account_id 
    set_tentant_id(current_account_id) 
    end 

    def set_tentant_id 
    #your logic 
    end 
end 

我希望它會工作得非常乾淨,精細.....