Error: undefined method `avatar?' for nil:NilClass

I am receiving the following errors in my rails app.

Error:
NoMethodError in Statuses#index

Showing C:/Users/Arvind/project/book/app/views/statuses/index.html.erb where line #12 raised:

undefined method `avatar?' for nil:NilClass

Rails.root: C:/Users/Arvind/project/book Application Trace | Framework Trace | Full Trace

app/helpers/applicationhelper.rb:28:in avatar_profile_link' app/views/statuses/index.html.erb:12:inblock in _app_views_statuses_index_html_erb__844883668_70166988' app/views/statuses/index.html.erb:9:in _app_views_statuses_index_html_erb___844883668_70166988' app/controllers/statuses_controller.rb:13:inindex'


My user.rb


 attr_accessible  :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name, :full_name, :avatar

has_attached_file :avatar, style: { large: "800x800>", medium: "300x200>", small: "260x180>", thumb: "80x80#" }

  def self.get_gravatars
     all.each do |user|
       if !user.avatar?
       user.avatar = URI.parse(user.gravatar_url)
            user.save
                print "."
              end
         end
     end 

My application_helper.rb

def avatar_profile_link(user, image_options={}, html_options={})

avatar_url = user.avatar? ? user.avatar.url(:thumb) : nil

link_to(image_tag(avatar_url, image_options),   profile_path(user.profile_name), html_options)

end


My lib/task/gravatars,rake


desc "Import avatars from user's gravatar url"
   task :import_avatars => :environment do
      puts "Importing avatars from gravatar"
         User.get_gravatars
            puts "Avatars updated."
      end

I updated My avatar to gravatar


$rake import avatar

Importing avatar to gravatar

.....Avatar updated.


My git repository is at: https://github.com/sarahgupta022/book.git


Thank You!

Answers


The problem is in /app/helpers/application_helper.rb in avatar_profile_link method, you have to check if user is nil. Replace with this code:

def avatar_profile_link(user, image_options={}, html_options={})
  avatar_url = nil
  unless user.nil?
    avatar_url = user.avatar? ? user.avatar.url(:thumb) : nil
    link_to(image_tag(avatar_url, image_options), profile_path(user.profile_name), html_options)
  end
end

The error message states:

undefined method `avatar?' for nil:NilClass

The avatar_profile_link helper method accepts a user argument. The value for user is nil at the point where the error occurs.

Looking at your git repo, the offending section is most likely here:

<% @statuses.each do |status| %>
<% if can_display_status?(status) %>
     <div class="status media">
        <%= avatar_profile_link status.user, {}, class: 'pull-left' %>

At least one status has a user of nil. That's about as far as we can see based on what you shared so for.


Need Your Help

Order (Slowest to Fastest) Big O running times

data-structures big-o time-complexity

Assuming that N is very large, can anybody help me in ordering the following list for Big O running times from Slowest to Fastest.

Is there any way to configure multiple registries in a single npmrc file

node.js npm

Here is my problem. We have a private NPM registry which only works in VPN. I would like to have a fallback registry https://registry.npmjs.org so that when I am out of VPN it works seamlessly.