Rails 5 Wicked-pdf 在新 window 中打开 pdf 从表单提交

Rails 5 Wicked-pdf open pdf in new window from a form submit

我创建了一种表单,可以根据按钮点击将数据保存在两个不同的 table 中,现在我可以将数据保存到两个不同的 table.

使用 Wicked 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

(猜测路径名)