Rails 3.1:将当前数据从 will_paginate 输出到 PDF

发布于 2024-12-14 10:20:57 字数 1102 浏览 4 评论 0原文

只要有时间,我已经使用 Rails 工作了几个星期了。我正在尝试创建应用程序当前显示的数据的 PDF 输出。我正在使用 prawn,它工作正常并且 will_paginate。是否可以将屏幕上显示的当前数据(例如所选页面)发送到 pdf 输出?

index.html.erb

<% @truckdeliveries.each do |truckdelivery| %>
 ...
<% end %>
<%= will_paginate @truckdeliveries %>
<%= link_to "PDF", truckdeliveries_path(@truckdelivery ,format: "pdf") %>

truckdeliveries_controller.rb

class TruckdeliveriesController < ApplicationController

def index
  @truckdeliveries = Truckdelivery.paginate page: params[:page], order: 'created_at desc',
  per_page: 10

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @truckdeliveries }
    format.pdf do
      pdf = TruckdeliveryPdf.new(@truckdeliveries)
      send_data pdf.render, filename: "truckdelivery_.pdf",
                                    type: "application/pdf"
    end
  end
end
...

这工作正常,但它只显示第一页的数据,如果我切换到另一个页面,它不会改变。我尝试将 link_to 更改为@truckdeliveries,但这只会创建一个神秘的链接。

我尝试过谷歌,但我仍然无法使用正确的流行语。因此,任何建议将不胜感激!

I've been working with rails for a few weeks now, whenever I had time. I'm trying to create a PDF output of the currently shown data of my application. I'm using prawn, which is working fine and will_paginate. Is it possible to send the current data, which is shown on the screen (e.g. the selected page), to the pdf output?

index.html.erb:

<% @truckdeliveries.each do |truckdelivery| %>
 ...
<% end %>
<%= will_paginate @truckdeliveries %>
<%= link_to "PDF", truckdeliveries_path(@truckdelivery ,format: "pdf") %>

truckdeliveries_controller.rb:

class TruckdeliveriesController < ApplicationController

def index
  @truckdeliveries = Truckdelivery.paginate page: params[:page], order: 'created_at desc',
  per_page: 10

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @truckdeliveries }
    format.pdf do
      pdf = TruckdeliveryPdf.new(@truckdeliveries)
      send_data pdf.render, filename: "truckdelivery_.pdf",
                                    type: "application/pdf"
    end
  end
end
...

This is working fine, but it only shows the data for the first page and doesn't change if I switch to another page. I tried changing the link_to to @truckdeliveries but that only creates one cryptic link.

I tried google but I'm still having trouble using the right buzz words. So any suggestions would be very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2024-12-21 10:20:57

在我看来,它只打印第一页的原因是因为您的 PDF 链接不包含页面参数。试试这个吧。

<%= link_to "PDF", truckdeliveries_path(:format => "pdf", :page => params[:page]) %>

It seems to me the reason that it is only printing the first page is because your PDF link isn't including the page parameter. Try this instead.

<%= link_to "PDF", truckdeliveries_path(:format => "pdf", :page => params[:page]) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文