2010-05-14 52 views
3

輔助方法current_user定義和ApplicationController提供作爲輔助這樣的:使用的郵件一個輔助方法是在控制器中定義

class ApplicationController < ActionController::Base 
    helper_method :current_user 
    def current_user 
     return @current_user if defined?(@current_user) 
     @current_user = current_user_session && current_user_session.record 
    end 
end 

我的問題是如何利用current_user幫手方法在郵件模板中(顯然它總是會返回nil,但我試圖渲染一個依賴於它的部分)。

通常當我想在一個郵件使用的助手,我像做add_template_helper(SongsHelper),但因爲助手在一個類中,而不是一個模塊定義我不知道該怎麼做

回答

4

我喜歡傳用戶在一個變量中,然後在模板中使用它。它將current_user呼叫傳遞給控制器​​(我通常從中發送郵件)。

另一種解決方案是將current_user和支持方法放到一個模塊中,並將它混合到ApplicationController中,並且還可以在繼承自ActionMailer :: Base的類中使用helper方法。如果你對這種方法感興趣,請看the docs

+0

我在一個郵件程序方法參數中傳遞了控制器方法的返回值。 'UserMailer.invitation(@invitation,current_organization).deliver'其中current_organization在'ApplicationController'中定義 – 2012-08-15 11:10:41

相關問題