wicked_pdf gem を使用して pdf ファイルを生成しています。
オブジェクトがデータベースに永続化/保存されている場合にのみ、pdf を生成したい:
このようなもの:
def new
@invoice = Invoice.new
respond_to do |format|
format.html # new.html.erb
end
end
def create
@invoice = Invoice.new(invoice_params)
respond_to do |format|
if @invoice.save
format.html { redirect_to invoice_path(@invoice), notice: "Yesss!") }
else
format.html { render action: new, :alert => "Oops :(" }
end
format.pdf do
@example_text = "exampletext"
render :pdf => "file_name",
:template => 'path/to/template'
end
end
end
ただし、このコードでは html にのみ応答します。次の内部でpdfを生成したい:
if @invoice.save
#generate the pdf here
format.html { redirect_to invoice_path(@invoice), notice: "Yesss!") }
else
どうすればいいですか、コールバックを使用できますか?