All threads / PDF Receipts not working

Ask A Question


You’re not receiving notifications from this thread.

PDF Receipts not working

Adrian DeGus asked in Rails

I just finished working through the pdf receipts video but can't get it to work. I have the webhook working and all charges are displayed properly, but the link to the pdf 404's.

Can you see where I went wrong?

# config/initializers/stripe.rb

class RecordCharges
  def call(event)
    charge =

    user = User.find_by(stripe_id: charge.customer)

                    amount: charge.amount,
                    card_last4: charge.source.last4,
                    card_type: charge.source.brand,
                    card_exp_month: charge.source.exp_month,
                    card_exp_year: charge.source.exp_year

StripeEvent.configure do |events|
  events.subscribe 'charge.succeeded',

# charge.rb

belongs_to :user

def receipt
        id: id,
        product: "Nuvro",
        company: {
            name: "Nuvro LLC",
            address: "2040 S Alma School Rd Suite 1-484\nChandler, AZ 85286",
            email: "[email protected]",
            logo: Rails.root.join("app/assets/images/nuvro-50.png")
        line_items: [
            ["Date",           created_at.strftime('%B' '%d,' '%Y')],
            ["Account Billed", "#{user.first_name} #{user.last_name} (#{})"],
            ["Product",        "Nuvro"],
            ["Amount",         "$#{amount / 100}.00"],
            ["Charged to",     "#{card_type} (**** **** **** #{card_last4})"],
            ["Transaction ID", uuid]

    # user.rb

    has_many :charges

# charges_controller.rb

def show
    @charge = current_user.charges.find(params[:id])
    respond_to do |format|
      format.pdf {
        send_data @charge.receipt.render,
                  filename: "#{@charge.created_at.strftime("%m-%d-%Y")}-nuvro-receipt.pdf",
                  type: "application/pdf",
                  disposition: :inline

    # routes.rb

    resource :charge # charges#show

    # views/companies/edit.html.erb

    <% current_user.charges.each do |charge| %>
    <%= link_to "View Receipt", charge_path(@charge, format: :pdf) %>

    <%= link_to "View Receipt", charge_path(charge, format: :pdf) %> # tried this, didn't work

I'm missing something obvious, I just know it :)

If the link is 404ing, then you either don't have routes, or your links are wrong. They should look something like this:

# config/routes.rb
resources :charges
<% current_user.charges.each do |charge| %>
    <%= link_to "View Receipt", charge_path(charge, format: :pdf) %> # tried this, didn't work
<% end %>

When I used resources :charges in my routes it threw an exception due to the lack of a charge ID. It's because of this that I changed my route to resource :charge

Well your snippet there was using @charge which would cause that because you didn't set the charge variable (and you can't since the user has multiple charges. You want resources plural because there are many charges and the user should be able to view each charge and the ID needs to be passed in to the charge_path. If you use both those that I mentioned you should be good.

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.