0

所以我试图通过路由之间的cookie传递一些字符串,但它们不断被重置并且它们不会被传递。这是我的代码:

post '/start_game' do
  @game = TicTacToe::Game.new
  cookies[:board] = @game.board.slots
  redirect '/game'
end

post '/game' do
  @slots = cookies[:board]
end

我也尝试过 request.cookies[:board],甚至使用引号而不是符号。

4

1 回答 1

0

I think this is because you are setting a cookie on one path and reading it on a different one. /start_game is considered to be a different part of your site to /game from a cookie point of view. You can solve this problem by setting the cookie at the / level:

response.set_cookie(:board, { :path => '/' })

You should not need to change the way that you read it back (because any other path is a sub-path of /).

于 2013-04-19T19:03:53.990 回答