簡體   English   中英

當我切換為使用form_with而不是form_tag時,Wicked_PDF無法下載

[英]Wicked_PDF does not download when I switch to using form_with instead of form_tag

我發現一個錯誤,該錯誤是在初次提交以生成PDF后無法提交我的表單,並且在研究過程中發現form_with是構建帶有Rails的表單的推薦方法。 我更新了表單,一切似乎都按照預期的方式運行,但是現在PDF不能像以前那樣下載,而只是在響應中呈現字符串而不生成文件。 使用Wicked-PDF和Rails 5.2.1。 抱歉,如果我缺少明顯的東西!

使用form_tag(預期的pdf下載):

表格形式:

<%= form_tag("download_pdf", format: :pdf, method: "get") do %>
    <div class="input-group mb-3">
      <%= collection_select(:location, :id, Location.all, :id, :name, {:include_blank => 'Filter by location'}, {:id => 'qr_code_loc_select', :selected_value => '', :name => 'location', :style=> 'width: 17em', :autocomplete => 'off'}) %>
    </div>
    <div class="input-group mb-3">
      <%= collection_select(:name, :id, DeviceType.all, :id, :name, {:include_blank => 'Filter by device type'}, {:id => 'qr_device_type_select', :selected_value => '', :name => "device_type", :style=> 'width: 17em', :autocomplete => 'off'}) %>
    </div>
    <div class="input-group mb-3">
      <input type="number" name="width" class="form-control label_size_input" id="label_width" step="0.001" placeholder="Label Width" autocomplete ='off' aria-describedby="basic-addon3">
      <div class="input-group-append">
        <span class="input-group-text">inches</span>
      </div>
    </div>
    <div class="input-group mb-3">
      <input type="number" name="height" class="form-control label_size_input" id="label_height" step="0.001" placeholder="Label Height" autocomplete ='off' aria-describedby="basic-addon3">
      <div class="input-group-append">
        <span class="input-group-text">inches</span>
      </div>
    </div>
    <%= label_tag "qr_col", "Columns:", class:"ml-2 text-light" %>
    <div class="input-group mb-3">
        <%= select_tag "qr_col", options_for_select(["1","2", "3", "4", "5"], "3"), {:autocomplete => 'off'}%>
    </div>
    <%= label_tag "paper_size", "Paper Size:", class:"ml-2 text-light" %>
    <div class="input-group mb-3">
        <%= select_tag "paper_size", options_for_select(@paper_size, "A4"), {:autocomplete => 'off'}%>
    </div>
    <div class="btn-group mb-3" role="group" aria-label="pdf_export_button" id="pdf_export">
      <%= hidden_field_tag :column_number %>
      <%= hidden_field_tag :label_size %>
      <%= hidden_field_tag :format, "pdf" %>
      <%= submit_tag("Create PDF Document", {:class=> "btn btn-secondary"}) %>
      <% end %>
    </div>

在控制器中:

def download_pdf
@columns = params[:column_number].to_i
@height = params[:height]
@width = params[:width]
@paper_size = params[:paper_size]
@type = params[:device_type]
@location = params[:location]
if @type.blank?
  if @location.blank?
    @devices = Device.all
  else
    @devices = get_devices_by_location(@location)
  end
else
  if @location.blank?
    @devices = Device.where('device_type_id=?', @type)
  else
    @devices = get_devices_by_type_and_location(@type, @location)
  end
end
pdf_string = render_to_string(
  template: "qr_codes/show.html.erb",
  layout: "layouts/pdf_layout.pdf.erb",
  viewport_size: '1280x1024',
  page_size: @paper_size
)
respond_to do |format|
  format.pdf do
    pdf = WickedPdf.new.pdf_from_string(pdf_string)
    send_data pdf, :filename => "report.pdf", :type => "application/pdf"
  end
end
end

在控制台中:

    Started GET "/qr_codes/download_pdf?utf8=%E2%9C%93&location=&device_type=&width=&height=&qr_col=3&paper_size=A4&column_number=3&label_size=&format=pdf&commit=Create+PDF+Document" for 127.0.0.1 at 2018-11-14 20:03:51 -0500
    Processing by QrCodesController#download_pdf as PDF
      Parameters: {"utf8"=>"✓", "location"=>"", "device_type"=>"", "width"=>"", "height"=>"", "qr_col"=>"3", "paper_size"=>"A4", "column_number"=>"3", "label_size"=>"", "commit"=>"Create PDF Document"}
      Rendering qr_codes/show.html.erb within layouts/pdf_layout.pdf.erb
       (0.4ms)  SELECT COUNT(*) FROM "devices"
      Device Load (0.6ms)  SELECT "devices".* FROM "devices"
      Rendered qr_codes/show.html.erb within layouts/pdf_layout.pdf.erb (778.0ms)
    "***************[\"/Users/joe/.rbenv/versions/2.5.0/bin/wkhtmltopdf\", \"-q\", \"file:////var/folders/pf/kl12j8g91w5bj7ssc_0fdv3c0000gn/T/wicked_pdf20181114-912-1opkhpt.html\", \"/var/folders/pf/kl12j8g91w5bj7ssc_0fdv3c0000gn/T/wicked_pdf_generated_file20181114-912-m57i12.pdf\"]***************"
      Rendering text template
      Rendered text template (0.1ms)
    Sent data report.pdf (1.7ms)
    Completed 200 OK in 3077ms (Views: 1.2ms | ActiveRecord: 1.0ms)

使用form_with(pdf不會按預期生成,並且僅包含未格式化的字符串作為響應):

表格形式:

 <%= form_with url: download_pdf_path(format: :pdf), method: "get" do |f| %>
    <div class="input-group mb-3">
      <%= f.collection_select(:id, Location.all, :id, :name, {:include_blank => 'Filter by location'}, {:id => 'qr_code_loc_select', :selected_value => '', :name => 'location', :style=> 'width: 17em', :autocomplete => 'off'}) %>
    </div>
    <div class="input-group mb-3">
      <%= f.collection_select(:id, DeviceType.all, :id, :name, {:include_blank => 'Filter by device type'}, {:id => 'qr_device_type_select', :selected_value => '', :name => "device_type", :style=> 'width: 17em', :autocomplete => 'off'}) %>
    </div>
    <div class="input-group mb-3">
      <%= f.number_field nil, {:name => "width", :id => "label_width", :class => "form-control label_size_input", :step => "0.001", :placeholder => "Label Width", :autocomplete => 'off'} %>
      <div class="input-group-append">
        <span class="input-group-text">inches</span>
      </div>
    </div>
    <div class="input-group mb-3">
      <%= f.number_field nil, {:name => "height", :id => "label_height", :class => "form-control label_size_input", :step => "0.001", :placeholder => "Label Height", :autocomplete => 'off'} %>
      <div class="input-group-append">
        <span class="input-group-text">inches</span>
      </div>
    </div>
    <%= label_tag "qr_col", "Columns:", class:"ml-2 text-light" %>
    <div class="input-group mb-3">
        <%= f.select "column_number", options_for_select(["1","2", "3", "4", "5"], "3"), {:autocomplete => 'off'}, :id => "qr_col"%>
    </div>
    <%= label_tag "paper_size", "Paper Size:", class:"ml-2 text-light" %>
    <div class="input-group mb-3">
        <%= f.select "paper_size", options_for_select(@paper_size, "A4"), {:autocomplete => 'off'}%>
    </div>
    <div class="btn-group mb-3" role="group" aria-label="pdf_export_button" id="pdf_export">
      <%= f.hidden_field :label_size %>
      <%= f.submit("Create PDF Document", {:class=> "btn btn-secondary", :data => { turbolinks: false }}) %>
    </div>
  <% end %>

控制器:

def download_pdf
@columns = params[:column_number].to_i
@height = params[:height]
@width = params[:width]
@paper_size = params[:paper_size]
@type = params[:device_type]
@location = params[:location]
if @type.blank?
  if @location.blank?
    @devices = Device.all
  else
    @devices = get_devices_by_location(@location)
  end
else
  if @location.blank?
    @devices = Device.where('device_type_id=?', @type)
  else
    @devices = get_devices_by_type_and_location(@type, @location)
  end
end
respond_to do |format|
  format.pdf do
    pdf = WickedPdf.new.pdf_from_string(
      render_to_string(
        template: 'qr_codes/show.html.erb',
        layout: 'layouts/pdf_layout.pdf.erb',
        page_size: @paper_size
      ),
    )
    send_data pdf, :filename =>'PDF Report-' + Time.now.strftime('%v %H:%M:%S').to_s, disposition: 'attachment', :type => "application/pdf"
  end
end
end

在控制台中:

    Started GET "/qr_codes/download_pdf_path?utf8=%E2%9C%93&location=&device_type=&width=&height=&column_number=3&paper_size=A4&label_size=&format=pdf&commit=Create%20PDF%20Document" for 127.0.0.1 at 2018-11-14 20:01:04 -0500
    Processing by QrCodesController#download_pdf as PDF
      Parameters: {"utf8"=>"✓", "location"=>"", "device_type"=>"", "width"=>"", "height"=>"", "column_number"=>"3", "paper_size"=>"A4", "label_size"=>"", "commit"=>"Create PDF Document"}
      Rendering qr_codes/show.html.erb within layouts/pdf_layout.pdf.erb
       (0.3ms)  SELECT COUNT(*) FROM "devices"
      Device Load (1.2ms)  SELECT "devices".* FROM "devices"
      Rendered qr_codes/show.html.erb within layouts/pdf_layout.pdf.erb (453.5ms)
    "***************[\"/Users/joe/.rbenv/versions/2.5.0/bin/wkhtmltopdf\", \"-q\", \"file:////var/folders/pf/kl12j8g91w5bj7ssc_0fdv3c0000gn/T/wicked_pdf20181114-912-1ym4a9q.html\", \"/var/folders/pf/kl12j8g91w5bj7ssc_0fdv3c0000gn/T/wicked_pdf_generated_file20181114-912-edkha0.pdf\"]***************"
      Rendering text template
      Rendered text template (0.1ms)
    Sent data PDF Report-14-NOV-2018 20:01:07 (1.6ms)
    Completed 200 OK in 2682ms (Views: 1.1ms | ActiveRecord: 1.5ms)

它有些愚蠢-決定將我的注意力轉向form_with文檔,並發現remote:true是默認設置。 如果將其設置為local:true,則此問題已解決。 希望對別人有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM