参数数量错误(2 代表 1),Rails 中的 UsersController [英] wrong number of arguments (2 for 1), UsersController in Rails
问题描述
我正在学习 Michael Hartl 的 Ruby on Rails 教程,虽然我已经能够为了自己解决一些错误,从我跋涉到第 9 章的这个错误让我绕了一圈.
I'm making my way through Michael Hartl's Ruby on Rails Tutorial and, while I have been able to troubleshoot a few errors on my own, this one from my trek through chapter 9 is sending me in circles.
我的views/users/index.html.erb
:
<% provide(:title, 'All users') %>
<h1>All users</h1>
<ul class="users">
<% @users.each do |user| %> # Line flagged with error
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
我的用户助手:
module UsersHelper
def gravatar_for(user, options = { size: 50 })
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
该错误还标记了我的文件中不存在的另一行代码;我猜它来自 Ruby 到 HTML 的转换?
The error also flags another line of code that doesn't exist in my files; I guess it comes from the Ruby to HTML conversion?
delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, :join, to: :to_a
我已经为此苦恼了将近两个小时,但没有任何进展.我所知道的是,当我用简单的文本替换索引页时,这些错误就会消失.这是 Gravatar 的问题吗?我真的不知道,希望得到任何帮助.
I've been scratching my head at this for almost two hours with no progress. All I know is that these errors go away when I replace the index page with simple text. Is this a problem with Gravatar? I really have no clue and would appreciate any help.
推荐答案
好的,您将许多参数传递给 gravatar_for
方法.将其重新定义如下,您可以照常使用它:
Ok, you pass to many args to the gravatar_for
method.
Redefine it as following and you can use it as you do:
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
您必须进一步阅读以了解如何重新定义该方法(它必须在某处).
You have to read further to see how to redefine the method (it must be somewhere there).
这篇关于参数数量错误(2 代表 1),Rails 中的 UsersController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!