如何通过某些参数生成我的PDF

时间:2015-06-16 13:19:24

标签: ruby-on-rails ruby ruby-on-rails-4

问题是我有一份报告,我发送给PDF我想缩小此报告的范围,因此我使用两个日期选择器来选择开始日期和结束日期,以便仅显示该日期范围的报告。现在我的PDF工作得很好,如果不包括我的参数,但是,这将打印所有内容,而不仅仅是我想要的数据,它位于我的参数[e]和[f]之间。任何帮助将不胜感激!!!!

[e] = startdate和[f] =结束日期

我希望能够生成我的PDF只显示所选日期范围的数据,但由于某种原因,我一直变为空。下面我将解释我正在采取的每一步尝试和努力实现这个。

所以这是我的PDF格式的控制器。

class EntryController < ApplicationController

 def print_version
    yr = DateTime.now
    jan = yr.beginning_of_year
    dec = yr.end_of_year

    #####These are my params
    e = params[:e] #start date 
    f = params[:f] #end date

    @labor_ticketer = LaborTicket.where("user_id = ? and type = ? and setup_completed = ? and workorder_type = ? and transaction_date >= ? and transaction_date <= ?", 'TIMEOFF', 'I', 'N', 'W', jan, dec)

    @labor_ticket = @labor_ticketer.where(:transaction_date => e..f) #looks at start and end and finds all between e(start) and f(end) 
    respond_to do |format|
      format.html #print_version.html.haml
      format.pdf do
        pdf = LaborTicketPdf.new(@labor_ticket, e, f)
        send_data pdf.render, filename: "laborticket.pdf",
                              type: "application/pdf",
                              disposition: "inline"
      end
    end
  end

******旁注!!!很抱歉,对于Labor_ticketer和labor_ticket以及LaborTicket的混淆,它们都是名为LaborTicket的模型。

这是我的LaborTicketPdf.rb

class LaborTicketPdf < Prawn::Document


   def initialize(labor_ticket, e, f) #tried to initialize params [e] and [f] here didn't work...
     super(top_margin: 20)
     @labor_ticket = labor_ticket
     title
     main_build
   end

   def title
     text "Labor Ticket Report"
   end

   def main_build
     move_down 20
     table transaction_rows do
       row(0).font_style = :bold
       columns(1..3).align = :right
       self.row_colors = ["DDDDDD", "FFFFFF"]
       self.header = true
     end
   end

   def transaction_rows
      [["Employee ID", "Date transfered", "Transaction Date(Requested Dates)", "Indirect ID"]] +
      @labor_ticket.map do |l|
         [l.employee_id, l.clock_out.strftime('%m/%d/%y'), l.transaction_date.strftime('%m/%d/%y'), l.indirect_id]
      end
    end
  end

这里的奖金是我开始[e]和结束[f]参数的地方。

这是我的print_version视图

 %h2 Date Search
 = form_tag print_version_entry_index_path, method: :get do
   = text_field_tag :e, params[:e], :class => 'datepicker', :placeholder => "Date Search"
   = text_field_tag :f, params[:f], :class => 'datepicker', :placeholder => "Date Search"
   = submit_tag "Search Date", :class=> "btn btn-primary"
   = link_to "Clear Search", print_version_entry_index_path, :class => "btn btn-success"


 - 2.times do
   %br

 %p= link_to "Printable Report (PDF)", print_version_entry_index_path(@labor_ticket, format: "pdf")

这里的更多奖励是它正在获得的sql

  LaborTicket Load (6.4ms)  SELECT "LABOR_TICKET".* FROM "LABOR_TICKET"  

  WHERE (user_id = 'TIMEOFF' and type = 'I' and setup_completed = 'N' and workorder_type = 'W' and transaction_date >= TO_DATE('2015-01-01 04:00:00','YYYY-MM-DD HH24:MI:SS') and transaction_date <= TO_DATE('2016-01-01 03:59:59','YYYY-MM-DD HH24:MI:SS'))

  AND ("LABOR_TICKET"."TRANSACTION_DATE" BETWEEN NULL AND NULL) 
  ################################################^ its getting null for params [e]start and [f]end 

  Rendered text template (0.0ms)
  Sent data laborticket.pdf (7.3ms)
  Completed 200 OK in 619ms (Views: 6.8ms | ActiveRecord: 253.1ms)

这是我的params传入时的样子,但由于某些原因,他们不希望在格式化为PDF时传递它。

  LaborTicket Load (79.5ms)  SELECT "LABOR_TICKET".* FROM "LABOR_TICKET"  

  WHERE (user_id = 'TIMEOFF' and type = 'I' and setup_completed = 'N' and workorder_type = 'W' and transaction_date >= TO_DATE('2015-01-01 04:00:00','YYYY-MM-DD HH24:MI:SS') and transaction_date <= TO_DATE('2016-01-01 03:59:59','YYYY-MM-DD HH24:MI:SS')) 

  AND ("LABOR_TICKET"."TRANSACTION_DATE" BETWEEN TO_DATE('2015-06-01','YYYY-MM-DD HH24:MI:SS') AND TO_DATE('2015-06-12','YYYY-MM-DD HH24:MI:SS'))

  # start(param[e] = 2015-06-01','YYYY-MM-DD HH24:MI:SS
  # end(param[f] = 2015-06-12','YYYY-MM-DD HH24:MI:SS

1 个答案:

答案 0 :(得分:1)

嗯,我认为这一切都非常简单!我所要做的就是改变这个......

%p= link_to "Printable Report (PDF)", print_version_entry_index_path(@labor_ticket, format: "pdf")

对此和presto !!!

%p= link_to "Printable Report (PDF)", print_version_entry_index_path(@labor_ticket, format: "pdf", :e => params[:e], :f => params[:f])