2013-10-11 26 views
0

在我的Rails应用程序中,我在我的页面控制器中定义了一个函数,它使用Nokogiri来为玩家和他们的排名刮一个网站。这个函数然后对它们进行排序并将结果放入一个数组中,该数组存储在我的@ladder实例变量中。Rails:实例变量似乎在Heroku上是空的,而不是在本地

然后在/ 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 

提前感谢!

-Mariogs

P.S.日志加入 “P @ladder” 到控制器方法后:

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 
    [email protected] do |l| 
     %li 
     = l 
+0

尝试在Heroku上调试应用程序:只要加上“P @ladder”到控制器方法和刷新页面后,再看看日志。 – alex

+0

请参阅上面的内容,了解我在日志中获得的内容 – bclayman

+0

似乎没有任何变化。编写视图和控制器代码。上面更新了 – alex

回答

0

好吧,p @ladder不火,引起Ladder.exists?是真正。

你确定heroku上的表不是空的吗?

尝试heroku run console然后Ladder.all.first

+0

这是完美的,感谢您的帮助! – bclayman

+0

不客气。如果有帮助,请接受我的回答。 – alex

相关问题