所以我刚刚创建了一个迁移文件,迁移它,并编写了我的“播放器”类。我正在尝试运行此代码:
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
不幸的是,当我运行代码时,Rails 给了我一个“错误数量的参数(3 代表 0..2)。所以有几件事:
1)这是我的播放器类:
class Player < ActiveRecord::Base
attr_accessible :name, :rating, :team
end
所以它应该需要 3 个参数来创建 Player 类的新实例。
2)我不知道为什么它显示“0..2”而不是普通整数。
3)另外,我现在得到“未初始化的常量 PagesController::Player.
这是我正在使用的 HAML 布局:
#ladder
%tr
%th Player
%th Rating
%th Team
%tr
-@ladder.each do |player|
%td player.name
%td player.rating
%td player.team
出于某种原因,它会打印出我的标题,但随后会一遍又一遍地打印出“player.name”、“player.rating”、“player.team”,而不是每个玩家的实际姓名、评分和团队......
想法?
很困惑,所以任何帮助都会很棒!
谢谢, 马里格斯