New Discussion

Notifications

You’re not receiving notifications from this thread.

How do I send Prawn pdf via email

3
Rails

Hello Everyone

how do i attaced a prawn pdf to email . I have implemented the prawn gem
so when you generate a new form and add the .pdf ot gives me the PDF , which works. but now when the form is created i want it to send an email with the PDF attached?


def new
    coach.build_referral(referrer: @current_referrer)

    respond_to do |format|
      format.html
      format.json
      format.pdf do
        pdf = BefriendingReferralForm.new(@current_referrer)
        send_data pdf.render, filename: 'referrer.pdf',
                              type: 'application/pdf',
                              disposition: :inline
      end
    end

but i would like to send an email when the form is submitted , i was thinking of doing this in the create method, This my current create method.


  def create
    coach.build_referral(referrer: @current_referrer)
    coach.assign_attributes(coach_params)

    if coach.save
        thinking adding code here?
      redirect_to referrers_root_path
    else
      render :new
    end
  end

Hey Neil!

Attachments are pretty easy. You use the attachments method on the email and give it a file object.

class ReferralMailer < ActionMailer::Base
  def referral(recipient)
    attachments['referral_form.pdf'] = BefriendingReferralForm.new(@current_referrer).render
    mail(:to => recipient, :subject => "New account information")
  end
end

Since you're rendering your own file, you just pass that in and Rails should take care of the rest.

ok cool thanks Chris

how would I call the ReferralMailer method in the my controller?
with the new and create action?

Just like you would do with sending any other email. You can read more on the guides page: http://guides.rubyonrails.org/action_mailer_basics.html

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 88,440+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.