简体   繁体   English

RoR App中错误的参数数量错误? 不确定为什么

[英]Wrong Number of Arguments Error in RoR App? Unsure why

So I've just created a migration file, migrated it, and written my "Player" class. 所以我刚刚创建了一个迁移文件,迁移了它,并编写了我的“Player”类。 I'm trying to run this code: 我正在尝试运行此代码:

def get_most_recent_ladder
    @top_80 = Team.all
    # loop through all teams, add each player and their rating to the hash, sort by rating, limit to 200
    all_players = []

    @top_80.each do |team|
      url = "http://modules.ussquash.com/ssm/pages/leagues/Team_Information.asp?id=#{team.team_id}"
      doc = Nokogiri::HTML(open(url))
      player_names = doc.css('.table.table-bordered.table-striped.table-condensed')[1].css('tr td a').map(&:content)
      player_ratings = doc.css('.table.table-bordered.table-striped.table-condensed')[1].css('tr td:nth-child(4)').map(&:content)
      for i in (0..player_names.length-1)
        player = Player.create(player_names[i], player_ratings[i].to_f, team.name)
        all_players << player
      end
    end

    all_players = all_players.sort{|player1, player2| player1.rating <=> player2.rating}.reverse.first(200)
    #insert creation of ladder object with order
    @ladder = all_players
    render 'ladder'
  end

Unfortunately, When I run the code, Rails gives me a "Wrong number of arguments (3 for 0..2). So a few things: 不幸的是,当我运行代码时,Rails给了我一个“错误数量的参数(3代表0..2)。所以有一些东西:

1) Here's my Player class: 1)这是我的Player类:

class Player < ActiveRecord::Base
  attr_accessible :name, :rating, :team
end

So it should need 3 arguments to create a new instance of the Player class. 所以它应该需要3个参数来创建一个Player类的新实例。

2) I don't know why it displays "0..2" instead of a normal integer. 2)我不知道它为什么显示“0..2”而不是普通整数。

3) Also, I'm now getting "Uninitialized Constant PagesController::Player. 3)另外,我现在得到“Uninitialized Constant PagesController :: Player。

Here's the HAML layout I'm using: 这是我正在使用的HAML布局:

#ladder
  %tr
    %th Player
    %th Rating
    %th Team
  %tr
    -@ladder.each do |player|
      %td player.name
      %td player.rating
      %td player.team

For some reason, it prints out my headings, but then literally prints out "player.name", "player.rating", "player.team" over and over again instead of each player's actual name, rating, and team... 由于某种原因,它打印出我的标题,但然后逐字打印出“player.name”,“player.rating”,“player.team”,而不是每个玩家的实际名称,评级和团队......

Thoughts? 思考?

Pretty confused so any help would be awesome! 很困惑所以任何帮助都会很棒!

Thanks, Mariogs 谢谢,Mariogs

The problem is your create call. 问题是你的create电话。 You need to supply the arguments as a hash: 您需要提供参数作为哈希:

player = Player.create(:name => player_names[i], :rating => player_ratings[i].to_f, :team => team.name)

This is because there is no way for Rails to know that the 3 parameters you supplied are supposed to match up to your 3 fields (and you should never assume Rails will keep the fields in that order anyway). 这是因为Rails无法知道你提供的3个参数应该匹配你的3个字段(你永远不应该假设Rails会保持字段顺序)。 By supplying a hash with specific keys (eg :name , :rating , etc.) Rails can properly match up your values with your fields. 通过提供具有特定键的哈希(例如:name:rating等),Rails可以将您的值与您的字段正确匹配。

When you want to display these values in your .haml files, use = before the items: 如果要在.haml文件中显示这些值,请在项目前使用=

%td= player.name
%td= player.rating
%td= player.team

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM