ArgumentError in UsersController#index wrong number of arguments (2 for 1)

1k views Asked by At

I am following Michael Hartl's Rails Tutorial and I am in Chapter 9, Section 3.

I am supposed to get:

enter image description here

But instead, I get:

enter image description here

Or, in plain text:

ArgumentError in UsersController#index

wrong number of arguments (2 for 1)

Extracted source (around line #4):

    # Returns the Gravatar for the given user.
      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")
Rails.root: /Users/TXC/code/microblog

Application Trace | Framework Trace | Full Trace
app/helpers/users_helper.rb:4:in `gravatar_for'
app/views/users/index.html.erb:7:in `block in _app_views_users_index_html_erb___2825860878015368470_70324743747280'
app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb___2825860878015368470_70324743747280'
Request

Parameters:

None
Toggle session dump

Here is the content of my User Helper:

module UsersHelper

  # Returns the Gravatar for the given user.
  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
end

And here is the User Controller:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update]
  before_action :correct_user,   only: [:edit, :update]

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      log_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

    # Before filters

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end
end

Any help to understand what is going wrong would be appreciated.

2

There are 2 answers

0
Thibaud Clement On BEST ANSWER

Following the advice found in this thread fixed the problem: Wrong number of arguments?

Here is the code that should be in the User Helper:

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
0
albatros On

According to the tutorial, the gravatar_for method is defined as

app/helpers/users_helpers.rb
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.
   app/helpers/users_helpers.rb
 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