I'm trying to set up a form that takes in a set of criteria, runs a query using those criteria, and either displays the results in the browser window below the form (via js) or as a pdf (using Prawn) in a new window.
index.html.erb (the form):
<%= form_for :criteria, url: results_path, method: :patch, remote: true do |f| %>
...input fields...
<%= button_tag("View", remote: true, value: 'js', name: 'format' %>
<%= button_tag("PDF", remote: true, value: 'pdf', name: 'format', formtarget: '_blank') %>
<div id='report-results'>
</div>
<% end %>
controller:
def index
end
def results
@results = ...the query...
respond_to do |format|
format.js
format.pdf do
report = ResultsPdf.new(@results)
send_data report.render, filename: "Results_#{Date.today}.pdf", type: 'application/pdf', disposition: "inline"
end
end
end
results.js.erb:
$('div#report-results').empty();
$('div#report-results').html("<%= escape_javascript(render 'report_results') %>");
With this code, the View option works (renders the results in the browser) but the PDF option appears to do nothing. The server shows it hitting the controller as PDF, running the query, and sending the data but the pdf itself is not generated. If I remove remote: true
from the form declaration then the PDF options works but the View option just renders the text of results.js.erb in the browser.