Save all user entries from html text built off a loop using ruby and sinatra

后端 未结 1 611
我寻月下人不归
我寻月下人不归 2021-01-15 06:42

I am creating a web page that displays a table. It also has four columns at the end of each record that are text fields where the user can enter data. When the user hits the

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 07:00

    By default when Sinatra (using Rack) parses the form data then if there are any duplicate keys then the last one is used and overwrites the others. However if you use a name that ends with [] then Sinatra instead creates an array containing all the entries with that name.

    So in your case you need to change the names of the input elements:

    
    

    (and similarly for the others). Note the [] at the end of the name.

    Now when you submit the form, params['event_description'] will be an array containing all the items submitted to the event_description input elements.

    The Sinatra/Rack query parsing is actually more sophisticated than this, so you can go further with your form. In your loop you could do something like this:

    
    
    
    
    

    Now when you submit the form and it is parsed, params['events'] will contain an array of hashes, each with keys description, type, class and issue_expert. You can then iterate over this array and process the data as appropriate. You may want to add a hidden id input with each hash to make sure each set of data is associated with the correct record (it might look something like ).

    0 讨论(0)
提交回复
热议问题