参数数目错误? [英] Wrong number of arguments?

查看:139
本文介绍了参数数目错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪迈克尔·哈特尔(Michael Hartl)的教程,此处,并尝试创建用户索引。

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

我的代码:

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

<%= 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?

推荐答案

根据本教程,gravatar_for方法定义为

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

请注意,它仅接受一个参数:用户。 第7章之后,在练习之后,本教程介绍了如何添加size参数:

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.

这篇关于参数数目错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆