2011-03-26 94 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' }" %>