2012-04-30 7 views
0

私は今月の収入を前月に比べて単純な月次報告書に電子メールで送付しようとしています。Rails 3アプリケーションの電子メールにコントローラメソッドを含める?

<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> 

I:私は、データを持つテーブルのために部分的に持っている

# 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) 

私は(臨床と呼ばれる別のモデルを参照している)私のレポートコントローラにその作業のすべてを持っていますその部分を私の電子メールに引き渡すことができるようにしたい、またはそれが不可能な場合は、最後の収入の価値を示したいと思います。

<%= @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

を実際に私が「ドン'@click_income_by_vet'がどこで初期化されているのかを見てください。' @click_income_by_month'だけです。ここには間違いがありますか? – alony

+0

うわー、よく目撃された。私は間違った質問を書いたことを示すために更新され、それは獣医でない@ clinical_income_by_monthを靴にするべきです。ありがとう。 – dannymcc

+0

'UserMailer.finance_report()'メソッドでビューに使用するすべての変数を初期化する必要があります – alony

答えて

0

あなたの@clinical_income_by_monthは明らかにnilです。つまり、選択クエリは、それが必要と考えるように値を返さないことを意味します。これはモデル問題です。ビューをチェックするのではなく、 "rails console"を起動して、クエリが必要な値を返すかどうかを確認する必要があります。コントローラで

user_mailer.rbで

def finance_report(clinical_income_by_month) 

    @clinical_income_by_month = clinical_income_by_month 

    ... # all you had here before 

end 

そしてこのようにそれを呼び出す:あなたはこのような何かする必要が

+1

リレーションがnilを返す場合、私は状況を見ません。結果がない場合、空のコレクションになります – alony

+0

@ clinical_income_by_month.firstはnilです。 []。first == nilと似ています。 – Chap

3

UserMailer.finance_report(@clinical_income_by_month).deliver 
+0

あなたのsuggesstionをuser_mailer.rbに追加し、提案されたコントローラコードを使ってレールコンソールから電子メールを送信しようとすると、 'Nil:NilClass'に対して 'ActionView :: Template :: Error:undefined method 'each'が表示されます。何か案は? – dannymcc

+0

何も変更されていませんか? – alony

関連する問題