2010-05-14 106 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

相关问题