nextstep
nextstep

Reputation: 43

getting an undefined method error from "_user.html.erb" in Rails 3

If I am getting this error

undefined method impressionist_count' for nil:NilClass after trying to use this line inside _user.html.erb

<%= @[email protected](&:impressionist_count) %>

where does it need to be defined? I already have this inside user.rb

def impressionist_count
    impressions.size
  end

I tried it in various helpers but to no avail

Upvotes: 0

Views: 294

Answers (2)

ericeason
ericeason

Reputation: 324

When you "render @users" each user is accessible to the partial as the local variable "user". Your partial should look like:

<%= user.impressionist_count+user.microposts.sum(&:impressionist_count) %>

Upvotes: 1

tadman
tadman

Reputation: 211560

The method is defined, but the @user object is not. This should've been assigned in the controller in order for it to work inside your view, or your partial should've been provided an object when you're rendering it.

Maybe you have something like:

<%= render(:partial => 'user') %>

What you might need is this if you have a user variable:

<%= render(:partial => 'user', :object => user) %>

Errors including the phrasing "for nil" are an indication of an undefined value being used incorrectly.

Upvotes: 0

Related Questions