2012-06-20 54 views
0

我有一个自定义控制器操作,如下所示。我有一个带有复选框的表单,它允许我通过使用Prawn将它们写入到pdf文件中来打印选定的项目。有任何想法吗?Rails 3.2控制器操作中的DoubleRenderError

我收到以下错误消息。

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' 

当我使用redirect_to的shipments_path我print_open方法结束时,它呈现视图,但在浏览器的URL仍读作:

http://localhost:3000/shipments/status/open 

代替:

http://localhost:3000/shipments 

回答

1

由于您正在遍历数组并在每个元素上调用send_data,因此您可能会多次呼叫send_data。那会给你你收到的例外。

+0

那么如何获得所需的输出?我需要send_data吗? – ctilley79

+0

你想要的输出是什么?一个带有项目列表的PDF文件? – robbrit

+0

我希望的输出是将pdf写入“public/bill_of_ladings”目录,然后刷新视图。到目前为止,它将在循环中编写第一个pdf。 – ctilley79