在我的应用程序中,我需要向供应商列表发送一封电子邮件。该电子邮件只需包含与该供应商有关的信息。例如,机柜详细信息和规格=机柜供应商,地板详细信息和规格=地板供应商等。数据库中的所有信息都来自一条记录。这个是可能的吗?到目前为止,RTFG (读取f google)还没有成功。如果可能,我可以在哪里找到和/或开始查找这方面的文档。谢谢!
发布于 2014-08-15 23:29:08
当然这是可能的。有两种方法:假设你没有为供应商定义模型,但只是从你正在谈论的记录中提取数据,你可以这样做:
class VendorMailer < ActionMailer::Base
  def self.send_mail(project)
    cabinet_vendor_mail.deliver(project)
    flooring_vendor_mail.deliver(project)
    # etc
  end
  def cabinet_vendor_mail(project)
    # set up your instance variables and send a mail using a view specific to this vendor type
  end
  # etc
end这就是大锤子的方法。最好是开发一个供应商类,然后在供应商和项目之间建立一个关系。
然后,您可以拥有一个适当的供应商邮件发送程序,它可以查看供应商类型并发送适当的消息类型。
发布于 2014-08-15 23:32:38
发送电子邮件的最基本方式是使用Rails的Action Mailer,http://guides.rubyonrails.org/action_mailer_basics.html。在您的示例中,您可能希望创建一个ActionMailer来处理要发送的各种模板,这取决于您要将它们发送给谁。所有这些都可以接受相同的变量,并在其模板中以不同的方式使用它们,因为它们是不同的模板。
创建一个包含您的电子邮件方法的ActionMailer。方法本身现在看起来是一样的,但那就是告诉Rails使用哪个模板。
class VendorMailer < ActionMailer::Base
  default from: 'notifications@example.com'
  def cabinet_email(vendor, specifications)
    @vendor = vendor
    @specifications = specifications
    mail(to: @vendor.email, subject: 'New Specifications')
  end
 def flooring_email(vendor, specifications)
    @vendor = vendor
    @specifications = specifications
    mail(to: @vendor.email, subject: 'New Specifications')
  end
end现在为以不同方式使用规范的每个方法创建一个模板。
app/views/vendor_mailer/cabinet_email.html.erb
<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>New specifications for you, <%= @vendor.name %></h1>
    <ul>
        <li><%= @specification.floor.width %></li>
        <li>...</li>
  </body>
</html>然后,在你的控制器中,只需循环发送各种电子邮件给所有需要通知的人。
class SpecificationsController < ApplicationController
  def create
    @specifications = Specifications.new(params[:specifications])
    respond_to do |format|
      if @specifications.save
        # Tell the VendorMailer to send emails after save
        # Look up correct vendor before these calls!
        VendorMailer.cabinet_email(vendor_cabinet, @specifications).deliver
        VendorMailer.flooring_email(vendor_flooring, @specifications).deliver
        format.html { redirect_to(@specifications, notice: 'Specifications were successfully created.') }
        format.json { render json: @specifications, status: :created, location: @specifications }
      else
        format.html { render action: 'new' }
        format.json { render json: @specifications.errors, status: :unprocessable_entity }
      end
    end
  end
end这些是一些基础知识,可以让你在不使用任何额外宝石的情况下找到正确的方向。
https://stackoverflow.com/questions/25328441
复制相似问题