0
class AdminMailer < ActionMailer::Base
  def send_message_to_user(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject)
  end

  def send_message_to_all_users(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject)
  end

  def send_message_to_active_users(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject)
  end

  def send_message_to_suspended_users(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject)
  end
end

我想为每个操作使用 4 个视图send_message_to_active_users.html.erb, send_message_to_all_users.html.erb, send_message_to_suspended_users.html.erb, send_message_to_user.html.erb,我只想对所有操作使用 1 个视图文件。

我该怎么做?

谢谢!

4

2 回答 2

1

要选择另一个模板,您可以:template_name作为选项传递:

mail(:to => user.email, :subject => subject, :template_name => "another_template_name")

因此,您可以将一个模板用于多种方法。

于 2013-03-26T18:35:07.160 回答
0

回应是:

class AdminMailer < ActionMailer::Base
  def send_message_to_user(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject) do |format|
      format.html { render "send_email_to_users" }
    end
  end

  def send_message_to_all_users(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject) do |format|
      format.html { render "send_email_to_users" }
    end
  end

  def send_message_to_active_users(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject) do |format|
      format.html { render "send_email_to_users" }
    end
  end

  def send_message_to_suspended_users(user, subject, body, locale)
    @body = body
    mail(:to => user.email, :subject => subject) do |format|
      format.html { render "send_email_to_users" }
    end
  end
end

你可以看看http://api.rubyonrails.org/classes/ActionMailer/Base.html

谢谢!

于 2013-03-26T18:35:55.797 回答