Ruby on Rails-Action邮件没有按照要求?

时间:2014-04-20 09:35:48

标签: ruby-on-rails ruby email ruby-on-rails-4 actionmailer

我有2个模型posend

po:has_many发送

发送:belongs_to po

send

send table

po

po table

send_controller

class SendsController < ApplicationController
 def new
 @send = Send.new
 end

def create
  @send = Send.new(params[:send])
if @send.save
     Pomailer.registration_confirmation(@send).deliver
    flash[:success] = "Send mail"
    redirect_to capax_path
else
    flash[:warning] = "mail not send"
    redirect_to capax_path
  end
end

pomailer

class Pomailer < ActionMailer::Base
   default from: "from@example.com"

  def registration_confirmation(po)
     @po = po

  mail(:to => "xyz@yahoo.co.in", :subject => " New purchase order send by " )


    end

   end

预期结果为邮件

enter image description here

如何从发送控制器将po的详细信息发送到邮件程序?

1 个答案:

答案 0 :(得分:1)

UNTESTED CODE:用它作为参考。

class SendsController < ApplicationController

def create
 @send = Send.new(params[:send])
 if @send.save
   Pomailer.registration_confirmation(@send.po).deliver
   flash[:success] = "Send mail"
   redirect_to capax_path
 else
   flash[:warning] = "mail not send"
   redirect_to capax_path
 end
end 

pomailer:

class Pomailer < ActionMailer::Base
 default from: "from@example.com"
 def registration_confirmation(po)
 @po = po
 mail(:to => "xyz@yahoo.co.in", :subject => " New purchase order send by " )
end

在视图中创建一个Pomailer文件夹并添加一个文件registration_confirmation.html.erb

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
 </head>
 <body>
  <h1>Dear <%= @po.vname %></h1>,
   <p>
   You have purchase order with invoice no <%= @po.invoiceno %>.
   </p>
  <p> 
  ......
   add your required information using @po object.
  .....
  </p>
  <p>Thanks !</p>
</body>
</html>

注意: 这是一个示例代码,我无法在不通过应用程序的情况下给出确切的工作代码,但我相信这会帮助您走得更远。