Rails Action Mailer:发送包含展示页HTML的电子邮件

时间:2016-07-23 21:25:15

标签: ruby-on-rails ruby actionmailer

我正在尝试使用动作邮件程序在结帐时向网站管理员发送订单电子邮件。在创建新费用但未发送完整消息时,将收到电子邮件。如何告诉邮件程序发送购物车展示页面的视图?

这是邮件程序html。我很确定这个@order_items行不正确但我不知道如何告诉邮件程序从购物车展示页面呈现HTML?目前电子邮件发送但它只发送您有一个新的订单行 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>

这是我的购物车展示页面,shopping_cart部分列出了所有的order_items。 车/ 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>

为控制器充电 class ChargesController&lt; 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

这是我的邮件:

class OrderMailer < ApplicationMailer
   add_template_helper(OrderItemsHelper)

  default from: "xxxx@gmail.com"

  def order_email(order_items)
     @order_items = order_items
       mail(to: "xxx@gmail.com", subject: "New Order")
  end
end

以下是我收到的错误:

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 个答案:

答案 0 :(得分:0)

如果您正在使用Rails 5,我认为您应该能够使用#{controller_name}Controller.renderer.render方法使用控制器的渲染器。

以下是另一种方法:

使用您当前的设置

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

注意我删除了html包装器,因为大多数情况下,它们已经在布局模板中定义了。

如果有效,请告诉我