9

I'm following Michael Hartl's tutorial here and am trying to create an index of users.

My code:

  class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:index, :edit, :update]
  .
  .
  .
  def index
    @users = User.all
  end
  .
  .
  .
  end

and

<%= provide(:title, 'All users') %>
<h1>All users</h1>

<ul class="users">
  <% @users.each do |user| %>
    <li>
      <%= gravatar_for user, size: 52 %>
      <%= link_to user.name, user %>
    </li>
  <% end %>
</ul>

I've made sure my code matches the code in the tutorial exactly, but I'm getting this error:

wrong number of arguments (2 for 1)

What am I doing wrong? Any thoughts?

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Danish M.
  • 997
  • 1
  • 10
  • 19
  • 1
    What line of code are you getting this error? Could you show your trace? – badams Jun 06 '12 at 14:53
  • 1
    Knowing what statement that's causing the error would be useful. But in essence you're passing two wrguments to a method that only takes one argument. – harald Jun 06 '12 at 14:55
  • **Solved the problem** turns out it was a mistake in `gravatar_for`. Sorry! – Danish M. Jun 06 '12 at 15:07

3 Answers3

24

According to the tutorial, the gravatar_for method is defined as

def gravatar_for(user)
  gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
  gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
  image_tag(gravatar_url, alt: user.name, class: "gravatar")
end

Notice that it only accepts one parameter: the user. Later in chapter 7, after the exercises, the tutorial describes how to add a size parameter:

# Returns the Gravatar (http://gravatar.com/) for the given user.
def gravatar_for(user, options = { size: 50 })
  gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
  size = options[:size]
  gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
  image_tag(gravatar_url, alt: user.name, class: "gravatar")
end

Judging by your error message, you haven't updated the method to use the optional size parameter.

rpedroso
  • 985
  • 6
  • 10
1

If you followed the tutorial and added the options hash to the function then you're only missing the {} around the options.

This should work. <%= gravatar_for user, {size: 52} %>

badams
  • 252
  • 3
  • 14
-1

Check here:

<%= gravatar_for user, :size => 52 %>
vicosanz
  • 57
  • 6