我正試圖通過電子郵件發送一份月度報告,該報告與上個月相比簡單了當月的收入。在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 => "£", :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 => "£", :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 => "£", :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報告中調用它。
任何指針,將不勝感激!
其實我不t看看「@ clinical_income_by_vet」初始化的位置,只有「@ clinical_income_by_month」。這是錯誤嗎? – alony
哎呀,很好看。更新以顯示我寫錯了問題,它應該鞋@clinical_income_by_month不審覈。謝謝。 – dannymcc
你應該在'UserMailer.finance_report()'方法 – alony