2014-09-23 49 views
2

我正在使用Pragmatic Agile Rails開發書練習ROR4。其中有一部分關於通過ActionMailer發送電子郵件。雖然我能夠發送電子郵件,我無法刪除eppended Rails的對象,如下圖所示,在突出區域:enter image description hereLineItem是一個模型,用於存儲量的Product買受人放入Cart爲什麼Rail Objects通過ActionMailer發送的電子郵件中出現


這些都是我的文件的內容:

// order_notifier.rb = my email sender model 
class OrderNotifier < ActionMailer::Base 
    default from: "[email protected]" 

    def received(order) 
    @order=order 
    mail to: @order.email, subject: 'Pragmatic Store Order Confirmation' 
    end 

    def shipped 
    mail to: "[email protected]" 
    end 
end 

// recieved.html.erb = the mail that will be sent 
<h3>PragmaticOrderShipped</h3> 
<p>This is just to let you know that we've shipped your recent order:</p> 
<table> 
    <tr> 
     <th>Description</th> 
     <th>Qty</th> 
    </tr> 
    <%[email protected]_items.each do |o_li|%> 
     <tr> 
      <td><%=Product.find(o_li.product_id).title%></td> 
      <td><%=o_li.quantity%></td> 
     </tr> 
    <%end%> 
</table> 

有人能告訴我,爲什麼是LineItem東西顯示在最後的電子郵件嗎?

回答

4

在你ERB文件,從@order.line_items.each除去=符號:

<% @order.line_items.each do |o_li| %> 

這將導致在除了執行塊要打印的模型對象。

+0

也可以改變'​​<%= Product.find(o_li.product_id).title僞%>''到​​<%= o_li.product.title%>'如果你想一些代碼重用的因素。 – 2014-09-23 13:50:41

+0

@DanFairaizl - thankx。那就是訣竅。 – zhirzh 2014-09-23 14:22:34

+0

@NitinVerma - 是的。我在提問產品belongs_to line_items的時候也忘了。傻我。 – zhirzh 2014-09-23 14:25:29