以下にリストされているカスタムコントローラーアクションがあります。選択した項目を Prawn を使用して pdf ファイルに書き込んで印刷できるチェックボックス付きのフォームがあります。何か案は?
次のエラーが表示されます。
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.
これがコントローラーの関連部分です
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
編集: send_data 行を削除することでエラーを取り除くことができますが、フォームを必要なページにリダイレクトすることはできません。おそらくこれがリモートフォームであるため、ディレクトリ内のjavacriptをレンダリングしたいだけです。次のルートを使用する form_tag を介して print_open を呼び出しています。
get '/shipments/status/:status' => 'shipments#index'
print_open メソッドの最後で redirect_to shippings_path を使用すると、ビューがレンダリングされますが、ブラウザーの URL は引き続き次のように表示されます。
http://localhost:3000/shipments/status/open
それ以外の:
http://localhost:3000/shipments