从内部操作视图邮件程序Rails 3访问SessionsHelper

时间:2011-08-23 09:01:06

标签: ruby-on-rails actionmailer

我有一个current_user,我签名如下:

SessionsHelper.rb

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.encrypted_salt] 
    current_user = user 
  end

def current_user
@current_user ||= user_from_remember_token
end

 private

  def user_from_remember_token
    User.authenticate_with_salt(*remember_token) #calls the remember_token method which returns an array containing two elements. the * works to pass the values of these two elements from the array.
  end

  #returns the array that is stored in the cookie in the browser
  def remember_token
    cookies.signed[:remember_token] || [nil, nil] #if there is no cookie then return an array of two elements both nil so program does not crash
  end

我想从我的动作邮件中访问current_user:

class ParticipantMailer < ActionMailer::Base
  add_template_helper(SessionsHelper)

更新以包含以下

 def welcome_Participant(participant)
      @participant = participant
      @user  = participant.user
      @board = participant.board
      @current_user = current_user
      @url   = "http://localhost:3000/#{participant.board.bp_name}/#{participant.token}"
      attachments.inline['logo.png'] = File.read(Rails.root.join('public/images/logo.png'))
      attachments.inline['cake.png'] = File.read(Rails.root.join('public/images/cake.png'))
      attachments.inline['btn-post-greeting.png'] = File.read(Rails.root.join('public/images/btn-post-greeting.png'))
      mail(:to => @user.email, :subject => "Welcome to Happy Birthday Greetings." )
    end

但这会产生以下错误:

ActionView::Template::Error (undefined method `cookies' for #<ParticipantMailer:0x00000102f91048>):

如何从我的邮件程序视图中访问current_user?

0 个答案:

没有答案