2013-06-13 101 views
0
get '/blackjack/*' do 
    if params[:splat] == "/hit" and defined? session[:bj_game] 
    erb :blackjack 
    elsif params[:splat] == "/fold" and defined? session[:bj_game] 
    session[:bj_hum].fold = true 
    erb :blackjack 
    else 
    if defined? session[:bj_game] 
     new_session_check 
     score_check 
     erb :blackjack 
    else 
     session[:bj_game] = false 
     session[:score] = 0 
     new_session_check 
     score_check 
     erb :blackjack 
    end 
    end 
end 

def new_session_check 
    if session[:bj_game] == false 
    session[:bj_hum] = Blackjack.new 
    session[:bj_com] = Blackjack.new 
    session[:bj_game] = true 
    end 
end 

def score_check 
    if session[:bj_hum].game_loop == false 
    if session[:bj_hum].score.to_i > 0 
     session[:score] += session[:bj_hum].score.to_i 
     check_save(session[:score]) 
    else 
     session[:score] = 0 
    end 
    session[:bj_game] = false 
    session[:bj_hum] = Blackjack.new 
    session[:bj_com] = Blackjack.new 
    end 
end 

每當它得到分數檢查,我都會得到NoM​​ethodError - 未定義的方法'game_loop'爲NilClass。從另一種方法訪問會話

但是,如果我從主頁開始:

get '/' do 
    session[:bj_game] = false 
    session[:score] = 0 
    erb :home 
end 

並點擊從家裏再培訓局的鏈接/酒杯上,它會工作作爲new_session_check看到變量是假的,然後創建新Blackjack類的實例(它具有for_g_accessor for game_loop)。

它爲什麼不在get'/ blackjack'版本中註冊?

http://pastebin.com/6EFpp5gh - 以下是您可以運行驗證此模型的版本。首先去localhost:4567/blackjack,你會得到一個內部服務錯誤。重新啓動服務器,然後先轉到localhost:4567,然後再轉到localhost:4567/blackjack,您將看到它的工作原理。

回答

0

你想要一個helper

get 'somepath' do 
    score_check 
end 

helpers do 
    def score_check 
    # now you can access session from within here 
    end 
end 
+0

希望這會工作,但同樣的事情。我去http:// localhost:4567/blackjack /手動獲取內部服務器錯誤,但是如果從'/'點擊Blackjack,它就會起作用。 – johnnycodami

+1

我做了這個樣機版本,有相同的錯誤:http://pastebin.com/6EFpp5gh運行它,然後直接到本地主機:4567/blackjack /它會得到一個內部服務器錯誤,然後重新啓動並進入本地主機: 4567然後到localhost:4567/blackjack /它不會得到那個錯誤。 – johnnycodami

相關問題