2012-04-30 75 views
0

我正試圖通過電子郵件發送一份月度報告,該報告與上個月相比簡單了當月的收入。在Rails 3應用程序的電子郵件中包含控制器方法?

我所有的工作在我的報告控制器(引用稱爲臨牀另一種模式):

# Clinical income by month report 
@clinical_income_by_month = Clinical.select(%q{date_format(transactiondate, '%Y') as year, date_format(transactiondate, '%M') as month, sum(LineBalance) as income}) 
           .where(:payments => 0) 
           .where('linebalance <> ?', 0) 
           .where('analysiscode <> ?', 213) 
           .group(:monthyear) 

然後我有一個部分用於與數據表:

<div class="row"> 
    <div class="twelve columns"><h5>This months income so far is <span style="font-size:1.2em"class="<% if @clinical_income_by_month.first.income >= @clinical_income_by_month.first(:offset => 12).income %>green<% else %>red<% end %> radius label"><%= number_to_currency(@clinical_income_by_month.first.income, :unit => "&pound;", :separator => ".", :delimiter => ",") %></span></h5> <%= @clinical_income_by_month.first.month %> <%= @clinical_income_by_month.first(:offset => 12).year %>'s income was <%= number_to_currency(@clinical_income_by_month.first(:offset => 12).income, :unit => "&pound;", :separator => ".", :delimiter => ",") %>.</div> 
    </div> 
<hr /> 

<table> 
    <tr> 
    <th>Year</th> 
    <th>Month</th> 
    <th>Income</th> 
    </tr> 
    <% @clinical_income_by_month.each do |c| %> 
    <tr> 
    <td><%= c.year %></td> 
    <td><%= c.month %></td> 
    <td><%= number_to_currency(c.income, :unit => "&pound;", :separator => ".", :delimiter => ",") %></td> 
    </tr>  
    <% end %> 
</table> 

我希望能夠將這部分內容放入我的電子郵件中,或者如果這不可能,我想顯示最後收入的價值。

<%= @clinical_income_by_month.first.income %> 

我的電子郵件地址代碼如下所示:

Finance Report 
================================================ 
<%= number_to_currency(@clinical_income_by_month.first.income) %> 

我得到的錯誤是:

1.9.2-p318 :009 > UserMailer.finance_report().deliver 
ActionView::Template::Error: undefined method `first' for nil:NilClass 
from /Users/dannymcclelland/Projects/premvet/app/views/user_mailer/finance_report.text.erb:3:in `_app_views_user_mailer_finance_report_text_erb___4501411113534248604_70358523362460' 

它的工作原理,當我拉開距離的標準方法的值:

<%= number_to_currency(Clinical.first.UserID) %> 

但不是當我從我創建的@clinical_income_by_month報告中調用它。

任何指針,將不勝感激!

+0

其實我不t看看「@ clinical_income_by_vet」初始化的位置,只有「@ clinical_income_by_month」。這是錯誤嗎? – alony

+0

哎呀,很好看。更新以顯示我寫錯了問題,它應該鞋@clinical_income_by_month不審覈。謝謝。 – dannymcc

+0

你應該在'UserMailer.finance_report()'方法 – alony

回答

0

顯然你的@clinical_income_by_month是零。這意味着select查詢不會像您認爲的那樣返回值。這是一個模型問題。您應該啓動「rails控制檯」並查看查詢是否返回您想要的值,而不是檢查視圖。

+1

我沒有看到關係會返回零的情況。如果沒有結果 - 它將是一個空集合 – alony

+0

@ clinical_income_by_month.first爲零。 就像[] .first == nil。 – Chap

3

你需要做這樣的事情:

在user_mailer.rb

def finance_report(clinical_income_by_month) 

    @clinical_income_by_month = clinical_income_by_month 

    ... # all you had here before 

end 
控制器

而且這樣調用它:

UserMailer.finance_report(@clinical_income_by_month).deliver 
+0

當您將建議添加到user_mailer.rb,然後嘗試使用您建議的控制器代碼從rails控制檯發送電子郵件時,我得到'ActionView :: Template :: Error:undefined method'each'for nil:NilClass'。有任何想法嗎? – dannymcc

+0

所以沒有改變? – alony

相關問題