0
votes

I am trying to use action mailer to send an email of orders to the websites admin during checkout. The email get sent when the new charges is created but it is not sending the full message. How do I tell the mailer to send the view of the shopping cart show page?

Here is the mailer html. I am pretty sure this @order_items line is incorrect but I am not sure how to tell the mailer to render the HTML from the shopping cart show page? Currently the email sends but it only sends the You have a new order line.
order_email.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>You have a new order </h1>
    <p>
     <%= @order_items %> <br>
    </p>

  </body>
</html>

This is my shopping cart show page the shopping_cart partial lists all of the order_items. cart/show.html.erb:

<div class="shopping-cart">
     <%= render "shopping_cart" %>
     <h4 class="text-right">Subtotal: <span style="color: green"><%= number_to_currency current_order.subtotal %></span></h4>
     <h4 class="text-right">Shipping: <span style="color: green"><%= number_to_currency current_order.shipping %></span></h4>
      <h4 class="text-right">Order Total: <span style="color: green"><%= number_to_currency current_order.total %></span></h4>

    <%= link_to 'Checkout', new_charge_path %>>
    </div>

Charges controller class ChargesController < ApplicationController def new

end

def create
  # Amount in cents
   @amount = (current_order.total * 100).to_i
   OrderMailer.order_email(@order_items).deliver_now


  customer = Stripe::Customer.create(
    :email => params[:stripeEmail],
    :source  => params[:stripeToken]
  )

  charge = Stripe::Charge.create(
    :customer    => customer.id,
    :amount      =>  @amount,
    :description => 'Order',
    :currency    => 'usd'
  )

rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to new_charge_path

end

end

Here is my mailer:

class OrderMailer < ApplicationMailer
   add_template_helper(OrderItemsHelper)

  default from: "[email protected]"

  def order_email(order_items)
     @order_items = order_items
       mail(to: "[email protected]", subject: "New Order")
  end
end

Here is the error that I am getting:

OrderMailer#order_email: processed outbound mail in 46.1ms
Completed 500 Internal Server Error in 138ms (ActiveRecord: 2.5ms)

ActionView::Template::Error (Missing partial order_mailer/_shopping_cart, application_mailer/_shopping_cart with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/home/ubuntu/workspace/app/views"
  * "/usr/local/rvm/gems/ruby-2.3.0/gems/devise-4.2.0/app/views"
):
    1: 
    2: <div class="shopping-cart">
    3: 
    4:   <%= render "shopping_cart" %>
    5:  <h4 class="text-right">Subtotal: <span style="color: green"><%= number_to_currency current_order.subtotal %></span></h4>
    6:  <h4 class="text-right">Shipping: <span style="color: green"><%= number_to_currency current_order.shipping %></span></h4>
    7:   <h4 class="text-right">Order Total: <span style="color: green"><%= number_to_currency current_order.total %></span></h4>
  app/views/carts/show.html.erb:4:in `_app_views_carts_show_html_erb__237194399509057886_70276459395980'
  app/views/order_mailer/order_email.html.erb:7:in `_app_views_order_mailer_order_email_html_erb___3173067988541617898_70276461477720'
  app/mailers/order_mailer.rb:11:in `order_email'
  app/controllers/charges_controller.rb:10:in `create'


  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (32.5ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (7.1ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.1ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (140.8ms)
1

1 Answers

0
votes

If you're using Rails 5, I think you should be able to use your controller's renderer, using the #{controller_name}Controller.renderer.render method.

Here is another way you could do this:

With your current setup,

    <h1>You have a new order </h1>
    <%= render(template: 'carts/show', locals: {pass in your local variables, if any, here}) %>

Notice I removed the html wrappers, because most times, those are defined already within the layout template.

Let me know if that works