Ruby on Rails-Action mailer no as per desired?

霸气de小男生 提交于 2019-12-06 15:46:13

问题


i have 2 models po and send

po: has_many sends

send: belongs_to po

send

po

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

Expected outcome to be mail

How can i send the details of the po into the mailer from send controller ?


回答1:


UNTESTED CODE: use it as a reference.

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

end

In views create a Pomailer folder and add a file 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>

NOTE: This is a sample code ,, i cannot give u exact working code without going throught application,,But i am sure this will help you to go farther.



来源:https://stackoverflow.com/questions/23180632/ruby-on-rails-action-mailer-no-as-per-desired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!