2013-10-05 35 views
0

我想使用ActionMailer發送電子郵件。Rails的ActionMailer參數是一個字符串

這是invoice_mailer.rb

class InvoiceMailer < ActionMailer::Base 
    default from: "[email protected]" 
    def invoice_email(invoice) 
    @invoice = invoice 
    mail(:to => @invoice.workorder.contact.email, :subject => "nDeavor New Invoice") 
    end 
end 

這是控制器代碼:

def sendinvoice 
    @invoice = params[:invoice_id] 
    InvoiceMailer.invoice_email(@invoice).deliver 
    end 

這是視圖代碼:

<%= link_to 'Send Invoice', invoices_sendinvoice_path(:invoice_id => @invoice), :class => 'btn btn-primary' %> 

我得到的錯誤是從invoice_mailer的這一行開始:

mail(:to => @invoice.workorder.contact.email, :subject => "nDeavor New Invoice") 

這是錯誤:

undefined method `workorder' for "10":String 

感謝您的幫助!

回答

0

在您的sendinvoice方法中,您有@invoice = params[:invoice_id],它將10分配給@invoice,然後將其傳遞給InvoiceMailer.invoice_email

你可能想要的是@invoice = Invoice.find(params[:invoice_id])。這將爲您提供ID爲10的發票對象。

+0

感謝您的回答! – Reddirt

相關問題