0

我正在尝试通过电子邮件发送月度报告,该报告与过去几个月相比很简单。

我在我的报表控制器中完成了所有这些工作(它引用了另一个名为 Clinical 的模型):

# 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 报告中调用它时。

任何指针将不胜感激!

4

2 回答 2

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
于 2012-05-01T07:14:46.033 回答
0

显然你的@clinical_income_by_month 是零。这意味着选择查询没有像您认为的那样返回值。这是一个模型问题。您应该启动“rails 控制台”并查看查询是否返回您想要的值,而不是检查视图。

于 2012-05-01T05:12:33.513 回答