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
每当得到分数检查时,我得到NoMethodError - NilClass的未定义方法'game_loop'。
但是,如果我从主页面开始:
get '/' do
session[:bj_game] = false
session[:score] = 0
erb :home
end
然后点击来自home erb的/ blackjack链接,当new_session_check看到变量为false然后创建了Blackjack类的新实例(其中有game_loop的attr_accessor)时,它将工作。
为什么不在get'/ blackjack'版本中注册?
http://pastebin.com/6EFpp5gh - 这是一个可以运行的模型版本来验证这一点。首先转到localhost:4567 / blackjack,您将收到内部服务错误。重新启动服务器,然后首先转到localhost:4567,然后转到localhost:4567 / blackjack,你会发现它有效。
答案 0 :(得分:0)
您需要helper:
get 'somepath' do
score_check
end
helpers do
def score_check
# now you can access session from within here
end
end