问题
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