Topic: Need help with 10.3 Michael Hartl's rails 3 toutorial

I am trying to create an index action and view for the User model.

In my users controller the index action is defined as:
def index
    @title = "All users"
    @users = User.all
end

And in the index.html.erb:
<ul class="users">
  <% @users.each do |user| %>
    <li>
      <%= gravatar_for user, :size => 30 %>
      <%= link_to user.name, user %>
    </li>
  <% end %>
</ul>

When I launch it in the browser I get an undefined method error:
undefined method `email' for nil:NilClass

I know gravatar_for searches its first parabiter for the value of the :email key so I am almost sure this is where it locks up. I tried
firing it up in the rails console.

>> users = User.all
returns an array of all my users

but when I try to itterate through all of them
>> users.each do |u|
>>    u.firstName
>> end
it shows no out put and the return is the same as above

I would greatly appriciate any help I am almost done with this toutorial and am really looking forward to learning the more creative aspects of rails.

Hope all is well

Last edited by tystick86 (2011-03-18 03:15:24)

Re: Need help with 10.3 Michael Hartl's rails 3 toutorial

Alight so I got it working but not the way the totorial said it should and I don't think it very rails but I am new. I would still like to hear some who really knows what their doing explain why the other way didn't work so I can start to under stand the deeper levels of this frame work.

I deleted the gravitar_for helper function from the view and it loaded with no errors.

The gravitar_for function looks like:
def gravatar_for(user, options = { :size => 50 })
    gravatar_image_tag(user.email.downcase, :alt => @user.firstName,
                                            :class => 'gravatar',
                                            :gravatar => options)
end

Now to get the gravitars in the index page all I had to do was make index.html.erb look like:
<ul class="users">
  <% @users.each do |u| %>
    <li>
      <%= gravatar_image_tag(u.email.downcase, :alt => u.firstName,
                                            :class => 'gravatar',
                                            :gravatar => {:size => 50}) %>
      <%= link_to u.userName, u %>
    </li>
  <% end %>
</ul>

And all the images now load up fine. But I am very confused why the tutorials code didn't work i.e.
<ul class="users">
  <% @users.each do |user| %>
    <li>
      <%= gravatar_for user, :size => 30 %>
      <%= link_to user.name, user %>
    </li>
  <% end %>
</ul>

I would really appreciate any help or ideas thank you for reading.

Re: Need help with 10.3 Michael Hartl's rails 3 toutorial

maybe it should be <%= gravatar_for @user %>     (notice the @user)

Re: Need help with 10.3 Michael Hartl's rails 3 toutorial

it's been some time since this was asked but ill post anyway.
the answer to this problem probably hides in users_helper.rb where gravatar_for is defined.
gravatar_for accepts user, options as arguments where probably you're calling gravatar_image_tag(@user.email.downcase, ....).
notice the @user should be user (gravatar_image_tag(user.email.downcase, ....)