Action Mailer 參數
提供參數郵件選項,以共用執行個體變數設定、處理和共通的標頭。
請考慮不使用參數化的範例
class InvitationsMailer < ApplicationMailer
def account_invitation(inviter, invitee)
@account = inviter.account
@inviter = inviter
@invitee = invitee
subject = "#{@inviter.name} invited you to their Basecamp (#{@account.name})"
mail \
subject: subject,
to: invitee.email_address,
from: common_address(inviter),
reply_to: inviter.email_address_with_name
end
def project_invitation(project, inviter, invitee)
@account = inviter.account
@project = project
@inviter = inviter
@invitee = invitee
@summarizer = ProjectInvitationSummarizer.new(@project.bucket)
subject = "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})"
mail \
subject: subject,
to: invitee.email_address,
from: common_address(inviter),
reply_to: inviter.email_address_with_name
end
def bulk_project_invitation(projects, inviter, invitee)
@account = inviter.account
@projects = projects.sort_by(&:name)
@inviter = inviter
@invitee = invitee
subject = "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})"
mail \
subject: subject,
to: invitee.email_address,
from: common_address(inviter),
reply_to: inviter.email_address_with_name
end
end
InvitationsMailer.account_invitation(person_a, person_b).deliver_later
使用參數化郵件,可以重寫成
class InvitationsMailer < ApplicationMailer
before_action { @inviter, @invitee = params[:inviter], params[:invitee] }
before_action { @account = params[:inviter].account }
default to: -> { @invitee.email_address },
from: -> { common_address(@inviter) },
reply_to: -> { @inviter.email_address_with_name }
def account_invitation
mail subject: "#{@inviter.name} invited you to their Basecamp (#{@account.name})"
end
def project_invitation
@project = params[:project]
@summarizer = ProjectInvitationSummarizer.new(@project.bucket)
mail subject: "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})"
end
def bulk_project_invitation
@projects = params[:projects].sort_by(&:name)
mail subject: "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})"
end
end
InvitationsMailer.with(inviter: person_a, invitee: person_b).account_invitation.deliver_later
命名空間
方法
- P
屬性
[W] | 參數 |
執行個體 public 方法
params() 連結
來源:顯示 | 在 GitHub 上
# File actionmailer/lib/action_mailer/parameterized.rb, line 95 def params @params ||= {} end