我有一个自定义控制器操作,如下所示。我有一个带有复选框的表单,允许我通过使用 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 shipping_path 时,它会呈现视图,但浏览器中的 url 仍然显示为:
http://localhost:3000/shipments/status/open
代替:
http://localhost:3000/shipments