2016-07-23 61 views
0

我正嘗試使用操作郵件程序在結帳時向網站管理員發送訂單電子郵件。創建新費用後發送電子郵件,但未發送完整消息。如何告訴郵件發送購物車顯示頁面的視圖?Rails操作郵件程序:用顯示頁面發送電子郵件HTML

這裏是郵件的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> 

收費控制器 類ChargesController < ApplicationController的 高清新

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: "[email protected]" 

    def order_email(order_items) 
    @order_items = order_items 
     mail(to: "[email protected]", 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) 

回答

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包裝,因爲大多數時候,那些已經在佈局模板中定義。

讓我知道是否可行

+0

我試過<%=渲染(模板:「車/秀」)%>,但我得到的是說,丟失的錯誤部分order_mailer/_shopping_cart我的購物車顯示頁面確實有一個_shopping_cart部分。有什麼我應該添加到代碼,所以它會發現部分呢? – Kris

+0

是的,大多數情況下,在渲染方法中明確表示總是更好。您應該可以通過從'<%= render「shopping_cart」%>'更改爲'<%= render「carts/shopping_cart」%>' – oreoluwa

+0

解決該問題。 – oreoluwa

相關問題