Here is some background on why I encountered this situation:
In my application, users have the ability to make changes in a form and save it. They can also generate a PDF with these changes. Previously, users had to save the changes, reopen the dialog, and then print the updated data.
This process seemed inefficient to me, so I simplified it to one action. Now, users can save the changes and close the form. If they want to print the changes, they just need to click the print button, which will save the data and automatically print the document.
Initially, my form contained two buttons that were sent via AJAX with remote: true
. One button was for submitting and saving the changes, while the other button with value=print
was meant to save the changes, generate the PDF, send it to the browser, and close the form.
This system functioned adequately, but aesthetically, it did not meet my standards.
The Form
<%= form_for @product, remote: true do |f| %>
<!-- Some fields omitted -->
<button class="btn btn-default glyphicon glyphicon-print" type="submit" value="pdf" name="print"></button>
<button type="button" class="btn btn-default" data-dismiss="modal"><%=t('forms.close')%></button>
<button class="btn btn-primary" type="submit"><%=t('forms.save')%></button>
<% end %>
These are the two submit buttons within the form.
The Controller Actions
def show
# This action generates the PDF and renders it in the browser
render_pdf PdfEngine.render params
end
def update
@product = Product.find params[:id]
# Logic for updating goes here
render :create
end
The partial create.js.erb
<% if params[:print] %>
location.href='<%=product_path(@product)%>'
<% else %>
// Additional actions such as closing the form may be performed here
<% end %>
Is there a more efficient way to save and render the PDF?