Reputation: 13558
I'm getting this error:
NoMethodError in Videos#index
Showing /rubyprograms/dreamstill/app/views/videos/index.html.erb where line #12 raised:
undefined method `upvoted_videos' for nil:NilClass
Extracted source (around line #12):
9: <div class="home_options">
10: <ul class="home_options_list">
11: <li> <%= link_to "Top Songs", videos_path, :class => "top_songs chosen_home_option" %> </li>
12: <li> <%= link_to "#{pluralize(current_user.upvoted_videos.count, 'Upvoted Song')}", upvoted_videos_path, :action => "upvoted_songs", :class => "upvoted_songs" %> </li>
This is in my user model:
def upvoted_videos
self.voted_videos.where("value = 1")
end
Upvotes: 0
Views: 1087
Reputation: 2885
Whenever you get a NoMethodError
always just go to check why, and it says that you are trying to call something on a nil
object.
This means you must not have a current_user
before doing this operation, so make sure someone is signed in.
Also it is good practice to have a fallback on functions like this, like a
<% unless current_user.nil? %> ...(Do function here)... <% end %>
or something in the view
Upvotes: 1
Reputation: 64177
It is throwing an error because you are trying to call upvoted_videos
on a nil object. This means that current_user
is nil. I would add some sort of check to see if current_user
is not nil before calling calling upvoted_videos
Upvotes: 2