2016-07-23 68 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

相关问题