2014-12-05 51 views
3

我無法收到一封電子郵件,以生成時我嘗試指定一個pdf我要附加到使用妖獸PDF電子郵件的佈局。這是一個rails 4應用程序。如何指定佈局時附加一個PDF到電子郵件使用妖獸PDF

當我使用下面的代碼,應用程序成功發送電子郵件的PDF附件,但沒有所需的樣式:

def invoice_created(invoice_id) 
    @invoice = Invoice.find(invoice_id) 
    @subscription = Subscription.find(@invoice.subscription_id) 
    attachments['invoice.pdf'] = WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "invoice", :template => 'invoices/show.pdf.erb')) 
    mail(to: '[email protected]', subject: "Your Invoice is Ready") 
end 

但是當我添加的佈局,沒有產生。

def invoice_created(invoice_id) 
    @invoice = Invoice.find(invoice_id) 
    @subscription = Subscription.find(@invoice.subscription_id) 
    attachments['invoice.pdf'] = WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "invoice", 
        :template => 'invoices/show.pdf.erb', 
        :layout => 'pdf.html.erb')) 
    mail(to: '[email protected]', subject: "Your Invoice is Ready") 
end 

在我的控制,我顯示在瀏覽器中的PDF文件,並正確地使用pdf.html.erb佈局:

def show 
    @invoice = current_account.invoices.find(params[:id]) 
    respond_to do |format| 
    format.html 
    format.pdf do 
     render :pdf => 'file_name', 
     :template => 'invoices/show.pdf.erb', 
     :layout => 'pdf.html.erb' 
    end 
    end 
end 

我沒有看到在開發日誌中的任何明顯的錯誤,除了電子郵件文件未被創建之外。

我看到下面的,所以它看起來像它看到的佈局,但不會表現出來。

Rendered invoices/show.pdf.erb within layouts/pdf.html.erb (13.7ms) 

難道存在佈局衝突的問題嗎?

如果我嘗試生成幾封電子郵件與指定的佈局,然後從郵件中刪除佈局代碼,重新啓動服務器,申請一個新的電子郵件,然後我先前請求的電子郵件被髮送。

我嘗試添加

:show_as_html => params[:debug].present? 

給寄件人,看它是否會提供一些線索,但是這也引起了不發送電子郵件。

+0

這是非常奇怪的。特別是具有的東西,它在結束時發送電子郵件。 idk,沒有正確關閉io流?您可以使用http://edgeguides.rubyonrails.org/4_1_release_notes.html#action-mailer-previews 查看生成的郵件您想在github上提交一些演示代碼嗎?我們可以檢查是否可以複製問題並向rails提交錯誤報告或wicked_pdf – 2014-12-05 12:20:45

+0

也可以通過使用render_to_string來檢查pdf是否可以保存到文件系統,這也是非常重要的https://github.com/mileszs/wicked_pdf#超高級使用 – 2014-12-05 12:25:25

+0

感謝您的想法發佈到github的例子。由於某種原因,我的配置中沒有提出錯誤。我建立的基本應用程序提出了問題,並立即解決。 https://開頭github上。COM/stevejc/WickedPDF-實施例 – Steve 2014-12-05 20:17:51

回答

3

問題的代碼是在PDF佈局:

<%= csrf_meta_tags %> 

不知道爲什麼我沒有在我的應用程序收到任何錯誤消息。我有我的應用程序設置爲顯示與gem 'better_errors'錯誤,這些錯誤沒有顯示任何錯誤。我成立了一個新的應用程序只是wicked_pdf玩,但沒添加better_errors到創業板上市的文件。

現在我接收到以下錯誤消息:

undefined method `protect_against_forgery?' 

一旦這條線是從layouts/pdf/html.erb文件的應用正確地附接在風格PDF除去。

下面就來wicked_pdf比如我以前找到解決方案的樣品鏈接:https://github.com/stevejc/WickedPDF-Example

相關問題