Ruby on Rails Wednesday, April 23, 2014

In my Rails app I have delayed_job gem to delay all the emails, the problem with this appear when I want to have some attachments in the emails, in development I store then in tmp folder.
I Heroku I thought that might be possible as well but I get the error the there is no file in that folder.

This is my code inside my controller: 

def send_bill
    @sale = Sale.find(params[:id])
    @client = @sale.client
    create_pdf(@sale, @client)
    PdfMailer.delay.send_pdf_info(@client, current_user.email)
    @bill.update(sent: true)
    flash.now.notice = "Bill was sent successfully"
    render :show
 end

The method create_pdf is the one that create the pdf and store it in the tmp folder.

def create_pdf(sale, client, is_bill = true)
    name = is_bill ? "factura.pdf" : "presupuesto.pdf"
    action = is_bill ? "bills/show" : "bills/budget"
    @sale = sale
    @client = client
    @bill = @sale.bills.first if is_bill
    file = File.new("#{Rails.root}/tmp/#{name}", "w+")
    kit = PDFKit.new( render_to_string(action: "#{action}", layout: 'pdf', format: "pdf"), :page_size => 'A4')
    file = kit.to_file(file.path)
 end

Inside my mailer the code is:

def send_pdf_info(client, current_user_email)
    @client = client
    attachments["Factura.pdf"] = File.read(#{Rails.root}/tmp/factura.pdf")

    mail(to: default_to(@client.email), bcc: current_user_email, subject: "Factura Printoria")
end


I'm not able to solve this.
Any help will be really appreciated. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b5a91850-0200-42e0-96da-e253f197fd54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment