2011-03-26 11 views
0

をデバッグするヘルプが必要です:は、私がログインしたとき、私はこのエラーを取得し、このエラーに

NoMethodError in Videos#index 

Showing /rubyprograms/dreamstill/app/views/videos/_video.html.erb where line #3 raised: 

undefined method `value' for nil:NilClass 

私のアプリケーションは、罰金、以前働いていたので、私はこのエラーを取得する理由は、私はわからないんだけど...ここ_video.html.erbですファイル。 3行目がエラーの原因です:

1: <%= div_for video do %> 
2: <div class='voting_div'> 
3: <%= link_to "&uArr;".html_safe, video_votes_path(:video_id => video.id, :type => "up"), :method => :post, :remote => true, :class => "up_arrow round #{current_user && current_user.votes_for(video).value == 1 ? 'voted' : 'unvoted' }" %> 
4: <div id='vote_display' class = 'round'> 
5:  <p id='votes'> 
6:  <%= video.vote_sum %> 

このエラーはどのように修正する必要がありますか?あなたが私にどのようなコードを投稿して、これをアイロンで仕上げることができますか?

答えて

0

current_user.votes_for(video)の空の応答が表示されます。また、current_user.votes_for(video)は結果がコレクションになることを意味します。それが真でない場合は、代わりにuser#vote_for(video)に変更します。

<%= link_to "&uArr;".html_safe, ... :class => "up_arrow round #{current_user && (vote = current_user.votes_for(video)) && vote.value == 1 ? 'voted' : 'unvoted' }" %> 
+0

すごいです!それはそれを固定!なぜあなたのコードは動作し、なぜ私の仕事はできませんでしたか? –

+0

'votes_for'の結果が返ってきました。 Mineはまず投票を行い、それがnilでない場合にのみ 'vote.value'を呼び出します。 – mnelson

+0

私は何をしているのかは分かりませんが、私は何をしていますか?2つのステップで、なぜ私はエラーが発生するのかわかりません... –

0

別の解決策は、これを次のようになります。

<%= link_to "&uArr;".html_safe, video_votes_path(:video_id => video.id, :type => "up"), :method => :post, :remote => true, :class => "up_arrow round #{current_user && current_user.votes_for(video).try(:value) == 1 ? 'voted' : 'unvoted' }" %>

関連する問題