需要帮助调试此错误

时间:2011-03-26 05:12:17

标签: ruby-on-rails ruby debugging ruby-on-rails-3

登录时出现此错误:

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

我该如何解决此错误?您希望我发布什么代码以便解决这个问题?

2 个答案:

答案 0 :(得分: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' }" %>

答案 1 :(得分: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' }" %>