我一直在寻找,但是找不到或者非常清楚该做什么,我确信它非常简单我只是对Ruby和Web应用程序很新,这个是在Sinatra中简单。我想将form_for
变量以及用户输入的内容传递到下一个结果页面的输入中,这样我就可以看到他们是否正确地输入了答案但是我不知道怎么做,我想我知道如何使用# web.rb
require 'sinatra'
def create_table(number)
possibles = [1,2,3,4,5,6,7,8,9,10,11,12]
int_set = possibles.shuffle
result = []
@answer_list = []
while int_set.length > 0
random_int = int_set[0]
string = (number.to_s + " x " + random_int.to_s)
int_set.delete(random_int)
if (random_int.to_s).length == 1
string += " = ______"
else
string += " = ______"
end
result << string
@answer_list << random_int.to_i * number.to_i
end
return result
end
get '/' do
@greeting = "Hi! Enter a number to generate a quick test."
erb :index
end
post '/results' do
erb :results
@multiplication_table
end
post '/' do
@multiplication_table = create_table(params[:int_to_generate])
@answers = @answer_list
erb :multiplication_table_page
end
执行此操作,但我不知道如何使用for循环执行此操作。
这是我的web.rb文件
<!-- multiplication_table_page.erb -->
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css">
</head>
<style>
h1 {text-align:center;}
p {text-align:center;}
.form-group {float: right; width: 100%;}
input {float: right;}
#submitall{width:100%;}
</style>
<body>
<div class="container">
<h1>Here is your table! Good luck.</h1>
<form action ="/results" method="POST" class="form-inline">
<% counter = 0 %>
<% @multiplication_table.each do |equation| %>
<input type="text" name = <%= "user_answer" + counter.to_s %> class="form-control">
<h2> <%= equation %> </h2>
<% counter += 1 %>
<% end %>
<p> </p>
<input id=submitall type="submit" class = "btn btn-primary">
</form>
<p> </p>
<p>Put another number in for another table</p>
<form action ="/" method="POST" class="form-inline">
<div class = "form-group">
<input type="submit" class = "btn btn-primary">
<input type="text" name ="int_to_generate" class="form-control">
</div>
</form>
<% @answers.each do |answer| %>
<p> <%= answer %> </p>
<% end %>
</div>
</body>
</html>
这是我的multiplication_table_page.erb
{{1}}