如何解决提交表单后,Rails 5 Wicked-pdf在新窗口中打开pdf
我创建了一种表单,可以通过单击按钮将数据保存在两个不同的表中,现在可以将数据保存到两个不同的表中。
使用邪恶的PDF创建pdf。
现在我想要当用户单击preview
按钮时,然后我想渲染我在create
动作中渲染的模板。当用户单击预览按钮时,我无法打开预览pdf。
index.html.erb
<%= form_with(model: @invoice,:html => {:id => 'invoice-form-validation'},url: [@payment_milestone,@invoice],local: true) do |form| %>
<div>
<%= form.submit ' Preview Invoice',name: 'preview_invoice_submit',:class=>"button secondary button-margin-top fa fa-eye" %>
<%= form.submit :class=>"button primary button-margin-top fa fa-files-o",name: 'invoice_submit'%>
</div>
<% end %>
invoices_controller.rb
def index
@invoices = @payment_milestone.invoices
@invoice = Invoice.new
end
def show
respond_to do |format|
format.html
format.json
format.pdf {render template: 'invoices/services_report',pdf: "services_invoice"}
end
end
def create
invoice_model = params[:invoice_submit] ? Invoice : TempInvoice
@invoice = invoice_model.new(invoice_params)
respond_to do |format|
if @invoice.save
format.html { redirect_to project_financial_payment_milestone_invoices_path,notice: 'Invoice was successfully created.' }
else
format.html { redirect_to project_financial_payment_milestone_invoices_path}
end
end
end
解决方法
提交或表单使用提交按钮的名称设置参数。
因此,您可以测试点击的“提交”按钮并可以在预览时重定向:
def create
invoice_model = params[:invoice_submit] ? Invoice : TempInvoice
@invoice = invoice_model.new(invoice_params)
if @invoice.save
if params.has_key?(:preview_invoice_submit)
redirect_to project_financial_payment_milestone_invoice_path(@invoice,format: :pdf)
else
redirect_to project_financial_payment_milestone_invoices_path,notice: 'Invoice was successfully created.'
end
else
redirect_to project_financial_payment_milestone_invoices_path
end
end
(猜测路径名)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。