【问题标题】:How to pass Rails form params to wicked_pdf如何将 Rails 表单参数传递给 wicked_pdf
【发布时间】:2018-05-24 07:12:15
【问题描述】:

我有这个动作,

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

使用此链接,

<%= 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 文档是空白的。并且调试显示表单参数没有被传递,因此查询正在获取空白。我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    您需要使用下面提到的从 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' %>
    

    此外,这一行的语法也不正确,它缺少 [ 在表单类参数中

    @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],)
    

    【讨论】:

    • 感谢您的更正和建议。但看来您正在硬编码这些值。你是?如果不是,那么它们是如何动态的?
    • 不,它们是动态的,您在 sample_card_report 中收到的参数需要从指向此操作的 link_to 发送。所以需要在link_to页面中有需要发送到sample_card_report的参数。
    【解决方案2】:

    如果您想将数据从控制器传递到 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
    

    然后像这样&lt;%= data %&gt;在views中打印出来

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      相关资源
      最近更新 更多