2012-06-20 79 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