Wrong number of arguments?

后端 未结 3 935
不思量自难忘°
不思量自难忘° 2021-01-04 22:31

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

My code:

  class UsersController < ApplicationController         


        
相关标签:
3条回答
  • 2021-01-04 22:50

    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.

    0 讨论(0)
  • 2021-01-04 22:50

    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} %>

    0 讨论(0)
  • 2021-01-04 22:50

    Check here:

    <%= gravatar_for user, :size => 52 %>
    
    0 讨论(0)
提交回复
热议问题