2011-03-26 137 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

哇!修復它!爲什麼你的代碼工作,爲什麼不工作? – 2011-03-26 05:21:55

+0

您收到'votes_for'的零結果。我先拉票,然後調用'vote.value'只有當它不是零。 – mnelson 2011-03-26 05:28:14

+0

好的,雖然我看不到它有什麼不同,但它是在做我所做的,但是在兩個步驟中,不知道爲什麼我會出現錯誤... – 2011-03-26 05:36:22

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' }" %>