在我的 Rails 应用程序中,我在 Pages 控制器中定义了一个函数,该函数使用 Nokogiri 为玩家及其排名抓取网站。然后,此函数对它们进行排序并将结果放入一个数组中,我将其存储在我的 @ladder 实例变量中。
然后在 /ladder 视图中,我遍历 @ladder 并创建一个有序列表。这发生在我的本地版本上,但是在我的 Heroku 版本上它什么也不显示。这让我觉得实例变量是空的。
当我将 binding.pry 放入本地版本时,实例变量不为空(正如预期的那样,因为梯形图页面正确呈现并显示列表)。
作为参考,这里是我的 Heroku 日志:
2013-10-11T21:11:38.843027+00:00 app[web.1]: Processing by HomeController#index as HTML
2013-10-11T21:11:38.848890+00:00 app[web.1]: Rendered home/index.html.haml within layouts/application (1.7ms)
2013-10-11T21:11:38.852468+00:00 app[web.1]: Rendered layouts/_nav.html.erb (1.1ms)
2013-10-11T21:11:38.853645+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.3ms)
2013-10-11T21:11:38.853645+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.3ms)
2013-10-11T21:11:38.852468+00:00 app[web.1]: Rendered layouts/_nav.html.erb (1.1ms)
2013-10-11T21:11:38.853968+00:00 app[web.1]: Completed 200 OK in 11ms (Views: 9.3ms | ActiveRecord: 0.0ms)
2013-10-11T21:11:38.843027+00:00 app[web.1]: Processing by HomeController#index as HTML
2013-10-11T21:11:38.848890+00:00 app[web.1]: Rendered home/index.html.haml within layouts/application (1.7ms)
2013-10-11T21:11:38.853968+00:00 app[web.1]: Completed 200 OK in 11ms (Views: 9.3ms | ActiveRecord: 0.0ms)
2013-10-11T21:11:39.062593+00:00 heroku[router]: at=info method=GET path=/assets/application-52afa7d9057584fe39fc9ea4de80358e.js host=rocky-brushlands-1229.herokuapp.com fwd="75.69.249.248" dyno=web.1 connect=3ms service=91ms status=200 bytes=916227
2013-10-11T21:11:38.863375+00:00 heroku[router]: at=info method=GET path=/ host=rocky-brushlands-1229.herokuapp.com fwd="75.69.249.248" dyno=web.1 connect=6ms service=396ms status=200 bytes=1485
2013-10-11T21:11:46.332091+00:00 app[web.1]: Started GET "/assets/knitting250px.png" for 75.69.249.248 at 2013-10-11 21:11:46 +0000
2013-10-11T21:11:46.332091+00:00 app[web.1]: Started GET "/assets/knitting250px.png" for 75.69.249.248 at 2013-10-11 21:11:46 +0000
2013-10-11T21:11:46.333297+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/assets/knitting250px.png"):
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in `block in tagged'
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:25:in `tagged'
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in `call'
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:38:in `call_app'
2013-10-11T21:11:46.333297+00:00 app[web.1]:
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/static.rb:64:in `call'
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in `tagged'
2013-10-11T21:11:46.333297+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in `block in call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/lock.rb:17:in `call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/content_length.rb:14:in `call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/engine.rb:511:in `call'
2013-10-11T21:11:46.333532+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:97:in `call'
2013-10-11T21:11:46.333794+00:00 app[web.1]: vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
2013-10-11T21:11:46.333794+00:00 app[web.1]:
2013-10-11T21:11:46.333794+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2013-10-11T21:11:46.333794+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/assets/knitting250px.png"):
2013-10-11T21:11:46.333794+00:00 app[web.1]: vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
2013-10-11T21:11:46.333794+00:00 app[web.1]:
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in `tagged'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in `call'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:25:in `tagged'
2013-10-11T21:11:46.333794+00:00 app[web.1]: vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in `block in call'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in `block in tagged'
2013-10-11T21:11:46.333794+00:00 app[web.1]:
2013-10-11T21:11:46.333794+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:38:in `call_app'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/static.rb:64:in `call'
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/lock.rb:17:in `call'
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/content_length.rb:14:in `call'
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
2013-10-11T21:11:46.333794+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:97:in `call'
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
2013-10-11T21:11:46.334611+00:00 app[web.1]:
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
2013-10-11T21:11:46.334395+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
2013-10-11T21:11:46.334611+00:00 app[web.1]:
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/engine.rb:511:in `call'
2013-10-11T21:11:46.334611+00:00 app[web.1]: vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
2013-10-11T21:11:46.335639+00:00 heroku[router]: at=info method=GET path=/assets/knitting250px.png host=rocky-brushlands-1229.herokuapp.com fwd="75.69.249.248" dyno=web.1 connect=2ms service=6ms status=404 bytes=728
2013-10-11T21:11:48.920310+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=rocky-brushlands-1229.herokuapp.com fwd="75.69.249.248" dyno=web.1 connect=2ms service=5ms status=200 bytes=0
2013-10-11T21:11:51.323199+00:00 app[web.1]: Started GET "/ladder" for 75.69.249.248 at 2013-10-11 21:11:51 +0000
2013-10-11T21:11:51.325258+00:00 app[web.1]: Processing by PagesController#ladder as HTML
2013-10-11T21:11:51.323199+00:00 app[web.1]: Started GET "/ladder" for 75.69.249.248 at 2013-10-11 21:11:51 +0000
2013-10-11T21:11:51.325258+00:00 app[web.1]: Processing by PagesController#ladder as HTML
2013-10-11T21:11:51.379144+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.0ms)
2013-10-11T21:11:51.377692+00:00 app[web.1]: Rendered pages/ladder.html.haml within layouts/application (2.2ms)
2013-10-11T21:11:51.378952+00:00 app[web.1]: Rendered layouts/_nav.html.erb (0.5ms)
2013-10-11T21:11:51.378952+00:00 app[web.1]: Rendered layouts/_nav.html.erb (0.5ms)
2013-10-11T21:11:51.377692+00:00 app[web.1]: Rendered pages/ladder.html.haml within layouts/application (2.2ms)
2013-10-11T21:11:51.379442+00:00 app[web.1]: Completed 200 OK in 54ms (Views: 5.5ms | ActiveRecord: 28.7ms)
2013-10-11T21:11:51.379442+00:00 app[web.1]: Completed 200 OK in 54ms (Views: 5.5ms | ActiveRecord: 28.7ms)
2013-10-11T21:11:51.379144+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.0ms)
2013-10-11T21:11:51.381483+00:00 heroku[router]: at=info method=GET path=/ladder host=rocky-brushlands-1229.herokuapp.com fwd="75.69.249.248" dyno=web.1 connect=1ms service=61ms status=200 bytes=1333
2013-10-11T21:11:28.381521+00:00 heroku[web.1]: Unidling
2013-10-11T21:11:28.381827+00:00 heroku[web.1]: State changed from down to starting
提前致谢!
-马里奥格斯
将“p @ladder”添加到控制器方法后的 PS 日志:
This is what I get in my logs after doing that:2013-10-11T22:43:49.549870+00:00 app[web.1]: Started GET "/ladder" for 75.69.249.248 at 2013-10-11 22:43:49 +0000
2013-10-11T22:43:49.551511+00:00 app[web.1]: Processing by PagesController#ladder as HTML
2013-10-11T22:43:49.559624+00:00 app[web.1]: Rendered pages/ladder.html.haml within layouts/application (0.7ms)
2013-10-11T22:43:49.561786+00:00 app[web.1]: Rendered layouts/_nav.html.erb (0.7ms)
2013-10-11T22:43:49.551511+00:00 app[web.1]: Processing by PagesController#ladder as HTML
2013-10-11T22:43:49.549870+00:00 app[web.1]: Started GET "/ladder" for 75.69.249.248 at 2013-10-11 22:43:49 +0000
2013-10-11T22:43:49.559624+00:00 app[web.1]: Rendered pages/ladder.html.haml within layouts/application (0.7ms)
2013-10-11T22:43:49.561786+00:00 app[web.1]: Rendered layouts/_nav.html.erb (0.7ms)
2013-10-11T22:43:49.562004+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.1ms)
2013-10-11T22:43:49.562004+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.1ms)
2013-10-11T22:43:49.562350+00:00 app[web.1]: Completed 200 OK in 11ms (Views: 5.1ms | ActiveRecord: 3.0ms)
2013-10-11T22:43:49.562350+00:00 app[web.1]: Completed 200 OK in 11ms (Views: 5.1ms | ActiveRecord: 3.0ms)
控制器代码:
def ladder
if Ladder.exists?
@ladder = Ladder.all.first
else
get_most_recent_ladder
end
end
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)
team_players_hash = {}
for i in (0..player_names.length-1)
team_players_hash[player_names[i]] = player_ratings[i].to_f
end
all_players = all_players.merge(team_players_hash)
end
all_players = all_players.sort_by{|_key, value| value}.reverse.first(200)
list = []
all_players.each do |player|
list << player[0]
end
#insert creation of ladder object with order
@ladder = Ladder.create(order: list)
render 'ladder'
p @ladder
end
查看代码:
#ladder
%ol
-@ladder.order.each do |l|
%li
= l