2013-10-11 52 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

相關問題