ruby - Sinatra / Rails : How to pass user input between views -
i've been looking bit , can't find or rather understand do, i'm sure quite simple i'm new ruby , web apps, 1 in sinatra simplicity. i'd pass @multiplication_table
variable things user puts inputs next results page can see if put in answers correctly dont know how to, think know how form_for
dont know how having loop.
this web.rb file
# 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 '/' @greeting = "hi! enter number generate quick test." erb :index end post '/results' erb :results @multiplication_table end post '/' @multiplication_table = create_table(params[:int_to_generate]) @answers = @answer_list erb :multiplication_table_page end
and multiplication_table_page.erb
<!-- 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 table! luck.</h1> <form action ="/results" method="post" class="form-inline"> <% counter = 0 %> <% @multiplication_table.each |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 number in 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 |answer| %> <p> <%= answer %> </p> <% end %> </div> </body> </html>
Comments
Post a Comment