【发布时间】:2013-05-21 20:05:41
【问题描述】:
我正在使用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
我该怎么做,我可以使用回调吗?
【问题讨论】:
-
您想要在 HTML 文档中嵌入 PDF 文件,还是只想呈现 PDF?如果是后者,您想将 format.save 更改为您的 pdf 块而不是 .html,我相信它会起作用。
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 wicked-pdf