Home Iterating through multiple fields on form post data
Reply: 0

Iterating through multiple fields on form post data

user1250
1#
user1250 Published in May 27, 2018, 3:37 am

In my rails app, I currently am able to get form post data from a two form fields of name="start" and name="end". The post data is as follows:

start: 2018-05-18 12:00
end: 2018-05-18 12:00

Here is the code that takes the post data and does something with it.

post "/time" do
  time = DoTime.new
  start = params["start"]
  finish = params["end"]
  time.started_at = start
  time.finished_at = finish
  time.profile = current_user
  time.save
  redirect "/profile"
end

I have since implemented a dynamic form which allows the user to duplicate the start and end fields as many times as they want. All this does is add the HTML form fields in place before submission and posts it with the form. An example of the new post data is here:

start: 2018-05-18 12:00
end: 2018-05-18 12:00
start: 2018-05-19 12:00
end: 2018-05-19 12:00

I am wondering how I can loop through the ruby to look for each instance of start and end and run the code above for each of the elements it finds. Any pointers or advice? I have tried a few different ways but keep running into errors.

Thanks

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303296 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO