繁体   English   中英

当我切换为使用form_with而不是form_tag时,Wicked_PDF无法下载

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

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我发现一个错误,该错误是在初次提交以生成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)
1 个回复

它有些愚蠢-决定将我的注意力转向form_with文档,并发现remote:true是默认设置。 如果将其设置为local:true,则此问题已解决。 希望对别人有帮助!

2 使用form_tag路由

现在,我试图在index.html.erb(members)页面上设置一个表单。 我希望表单显示搜索结果,并且不要将其移至其他页面。 我想在index.html.erb(members)本身上显示结果。 但是下面的代码使应用程序在按下搜索按钮后移至show.html(members)。 ...

4 当我提交form_tag时,什么也没发生

我目前正在尝试为与客户表格相关联的地点实现搜索栏。 当我按Enter键时,我希望页面选择与输入匹配的所有位置。 我的form_tag看起来像这样: 我的位置控制器: 我的路线 耙路线如下所示: 因此,当我尝试获取某些内容时,我的看法保持不变,但网址看起 ...

8 使用form_tag时缺少Require对象

我有一个表单发送这样的表单数据: 在控制器中,我检查参数: 使用表单时,出现以下错误: 实际上, POST参数中缺少它: 我希望这样: 怎么了 通过在form_tag使用Pturnover.new ,我希望可以使用适当的require参数? ...

10 使用Form_tag插入记录时出现问题

我有一个新问题。我想向数据库插入一条记录。 没有错误发生,但记录未成功保存。我的模型名称是副词,一个字段的名称是name。这是我的代码: 在我的路线上 和控制器: 谢谢你的帮助 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2021 STACKOOM.COM