我正在跟随Michael Hartl的
here教程,并试图创建一个用户的索引.
我的代码:
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>
wrong number of arguments (2 for 1)
我究竟做错了什么?有什么想法吗?
解决方法
根据教程,gravatar_for方法被定义为
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
请注意,它只接受一个参数:用户. Later in chapter 7,练习后,本教程介绍如何添加一个size参数:
# 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,class: "gravatar") end