2012-04-30 49 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

相关问题