使用 wicked_pdf,保存为包含用户提供的数据的 pdf

Using wicked_pdf, save as pdf including user supplied data

我有一个模板需要用户填写。我的目标是使用 wicked_pdf.

将模板转换为 pdf(包含用户提供的信息)

此外,none 的用户输入正在命中 model/database。我只需要将模板转换为包含用户数据的 pdf,仅此而已。

到目前为止,我可以将模板转换为 pdf、保存并重定向回我的索引视图。但是,用户填写的none个字段被保存;所有包含用户输入的输入或值的字段都是空白的。它只是保存一个空白模板。如何使用 wicked_pdf 将 pdf 和用户提供的数据转换为 pdf?

# controller save method --->
def save_grade_sheet
  @result = Result.find(params[:id])
  if current_user.admin?
    filename = "#{@result.user.last_name}_grades.pdf"      
    save_path = "#{Rails.root}/public/uploads/#{@result.user.last_name}/#{filename}"
    respond_to do |format|
      pdf = render_to_string pdf: filename, 
                            template: "/results/grade_sheet.html.erb", 
                            encoding: "UTF-8", 
                            disposition: "attachment", 
                            save_to_file: save_path, 
                            save_only: true
      File.open(save_path, "wb") do |file|
        file << pdf
      end
      flash[:notice] = "#{filename} successfully saved"
      format.html { redirect_to results_path }
    end
  else
    head :unauthorized
  end
end



# sample template code
<div>
  <div>
    <h2>PERSONNEL INFORMATION</h2>
    <p>Examinee's Name: <%= @result.user.first_name %> <%= @result.user.last_name %></p>
    <p>Examiner's Name: <input class="inline" type="text"></p>
  </div>
  <div>
    <p>Exam Type: <%= @result.test.upcase %></p>
    <p>Exam Version: <input class="inline" type="text"></p>
    <p>Exam Date: <%= @result.created_at.strftime("%m-%d-%y") %></p>
  </div>
  <%= button_to "Create Grade Sheet", save_grade_sheet_result_path(@result), data: { method: :post }, class: "btn btn-primary btn-lg"%>
</div>

我将所有内容发送到正确的路由,但 none 的数据实际上已被传递。相反,我需要将所有内容包装在 form_tag 中并使用 submit_tag.

<%= form_tag(save_grade_sheet_result_path(@result), method: :post) %>
  <div>
    <h2>PERSONNEL INFORMATION</h2>
    <p>Examiner's Name: <%= text_field_tag :examiner_name, @examiner_name, class: "inline" %></p>
    <p>Exam Version:<%= text_field_tag :exam_version, @exam_version, class: "inline" %></p>
  </div>
  <%= submit_tag "Create Grade Sheet", class: "btn btn-primary btn-lg" %>
<% end %>

在我的控制器中,我需要获取传入的参数:

def save_grade_sheet
  @result = Result.find(params[:id])
  @examiner_name = params[:examiner_name]
  @exam_version = params[:exam_version]
  if current_user.admin?
    # existing code
  end
end