Topic: Loop through form(Dynamic amount)

Hi there,

I'm very new to rails, so please keep that in mind a little :-p

Now; the question: I've got a form which dynamically adds more fields to the form whenever i click the "Add another field" button. I'm creating a POST array that way by naming the field titles[]. I think most of you know that HTML trick.

Anywho, i want all those titles to be saved to my DB(In one go, not through AJAX or something). But how do i loop through those titles?

Thanks in advance!

Last edited by CaptainCarl (2012-09-25 03:26:47)

Re: Loop through form(Dynamic amount)

bump

Re: Loop through form(Dynamic amount)

There are no html tricks and no looping necessary, and it makes no difference to the end result if you use ajax to post your form.
You need to make use of accepts_nested_attributes, form_for and fields_for.
Check out http://railscasts.com/episodes/196-nest … orm-part-1
and http://railscasts.com/episodes/197-nest … orm-part-2
Also you may well find it weorth your while to subscribe to watch http://railscasts.com/episodes/196-nest … rm-revised

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)