Я новичок в программировании, пишу простую игру «Камень, ножницы, бумага». есть 3 кнопки с картинками, в зависимости от того, какую из них нажмет игрок, на следующей странице должна появиться эта картинка. Другими словами, если игрок нажимает кнопку «рок», то это должно отображаться на следующей странице. Проблема в том, что я не могу понять, как сохранить выбранную кнопку, чтобы она отображалась в маршруте «/results».
(play.erb)
<div class = "pbox-1">
<form action=/choice method = "post">
<input type = "image" src = "images/rock.jpg" name = "rock" />
</form>
</div>
<div class = "pbox-2">
<form action=/choice method = "post">
<input type = "image" src = "images/paper.jpg" name = "paper" />
</form>
</div>
<div class = "pbox-3">
<form action=/choice method = "post">
<input type = "image" src = "images/scissors.jpg" name = "scissors" />
</form>
</div>
(controller)
require 'sinatra/base'
require './lib/game'
require './lib/player'
class Rps < Sinatra::Base
enable :sessions
get '/play' do
erb :play
end
post '/choice' do
redirect '/results'
end
get '/results' do
erb :results
end
run! if app_file == $0
end
(Player class)
require_relative 'game'
class Player
attr_reader :name, :choice
def initialize(name)
@name = name
@choice = nil
end
def make_choice(player_choice)
@choice = player_choice
end
end
(Game class)
require_relative 'player'
class Game
attr_reader :players
def initialize(player_1)
@players = player_1
end
def player_1
@players
end
def player_1_choice
player_1.choice
end
end






Вы можете поместить скрытое поле в каждую из форм, например:
<input type = "hidden" name = "choice" value = "rock" />
Затем в вашей функции post '/choice' вы можете получить к ней доступ с помощью params[:choice].