如何将 Rails 表单参数传递给 wicked_pdf

How to pass Rails form params to wicked_pdf

我有这个动作,

  def email_student_report
   @current_year = Time.new.year
   @past_year = @current_year - 1
  end

使用这个 link 到,

<%= link_to 'Email', samplecardreport_path(format: :pdf), :class => 'btn btn-warning termact_btns' %>

我将此称为此操作并使用 wicked_pdf

生成 PDF 文档
  def sample_card_report
   @subjects = Subject.all
   @properties = Property.first
   @studentid = Student.where('studentid = ?', params[:email_search_stuent_id]).last!
   #@studentid = Student.where('studentid = ?', 'EkGSS-181').last!
   @studentresults = TermReport.where('studentid =? and term =? and form_class =? and year =?', params[:email_search_stuent_id], params[:term], params[:form_class], params[:year])
   @totalstudent = TermReport.group('form_class, term, year, studentid').where('form_class =? and term =? and year =?', params:form_class],params[:term],params[:year],)

   respond_to do |format|
    format.html
    format.pdf do
     render pdf: 'student_report_card',
     template: "term_reports/sample_card_report.pdf.erb"
    end
   end
  end

现在我的问题是生成的 PDF 文档是空白的。调试显示没有传递表单参数,因此查询获取空白。我该如何解决?

您需要使用下面提到的 link_to 标签手动发送参数:

<%= link_to 'Email', samplecardreport_path(format: :pdf, param1: value1, param2: value2), :class => 'btn btn-warning termact_btns' %>

在你的情况下就是这些参数

<%= link_to 'Email', samplecardreport_path(format: :pdf, email_search_stuent_id: value1, term: value2,form_class: value3, year: value4), :class => 'btn btn-warning termact_btns' %>

此外,这一行的语法也不正确,缺少 [格式 class params

@totalstudent = TermReport.group('form_class, term, year, studentid').where('form_class =? and term =? and year =?', params:form_class],params[:term],params[:year],)

正确

@totalstudent = TermReport.group('form_class, term, year, studentid').where('form_class = ? and term = ? and year = ?', params[:form_class],params[:term],params[:year],)

如果您想将数据从您的控制器传递到 HTML 视图,您可以像这样在其中使用 locals 选项。

 respond_to do |format|
      format.html
      format.pdf do
        render pdf: "file_name",   # Excluding ".pdf" extension.
        template: "students/pdf.html.erb",
        locals: { data: "hello world"}
      end
    end

然后像这样在views中打印出来<%= data %>