I have a custom controller action as listed below. I have a form with checkboxes that allow me to print the selected items by writing them to a pdf file using Prawn. Any Ideas?
I’m getting the following error.
AbstractController::DoubleRenderError (Render and/or redirect were called multiple
times in this action. Please note that you may only call render OR redirect, and
at most once per action.
Here is the relevant part of the controller
class ShipmentsController < ApplicationController
autocomplete :client, :name, :full => true
autocomplete :product, :product_name, :full => true, :extra_data => [:product_code, :miller_product_code]
respond_to :js, :html
def index
if params[:status]
@openshipments = Shipment.where(:status => params[:status]).search(params[:search1]).order(sort_column + " " + sort_direction).page(params[:page])
else
@shipments = Shipment.search(params[:search]).order(sort_column + " " + sort_direction).page(params[:page])
end
end
def create
...
end
def edit
...
end
def update
...
end
def destroy
...
end
def print_open
@printshipments = Shipment.find(params[:open_shipment_ids])
respond_to do |format|
format.pdf do
@printshipments.each do |shipment|
pdf = Prawn::Document.new
pdf.text "bill_of_lading_#{shipment.bill_of_lading}"
#removed send_data pdf.render, filename: "bill_of_lading_#{shipment.bill_of_lading}"
File.open("public/bill_of_ladings/bill_of_lading_#{shipment.bill_of_lading}.pdf", "wb") { |f| f << pdf.render }
shipment.update_attribute(:status, "PRINTED")
end
end
end
redirect_to shipments_path
end
EDIT:
I can get rid of the error by removing the send_data line, however I can’t get the form to redirect to the page I want. It only wants to render the javacript in the directory, possibly because this is a remote form. I am calling the print_open through a form_tag that uses the following route.
get '/shipments/status/:status' => 'shipments#index'
When I use the redirect_to shipments_path at the end of my print_open method, It renders the view, but the url in the browser still reads as:
http://localhost:3000/shipments/status/open
instead of:
http://localhost:3000/shipments
Since you’re looping over an array and calling
send_dataon each element, it’s possible you’re callingsend_datamore than once. That would give you the exception that you’re receiving.