Help please Submitting multiple records from form..

Hi all,
i'v been trying to understand how can i add more then one record to the
DB from a form in a single shot so to say..
I have an ITEM module, it has an ItemID and TYPEofItem , the TypeofItem
is been selected from a dropdown list that populates the item types i
have in another module *(called Types)..
I have everything working nicly and i am adding the ItemID with no
problem,
but! i am trying to understand if i can type in more then one ItemID
(with the same ItemType value) and submitting them all at once to the
DB..
Maby crating a list object (i have no idea on how to do that in the
view..) and filling out rows with differnt ID's and then running on the
list and adding them all...
Can someone please help me understand how to do that ? - thank's...

Hanan Y. wrote in post #960326:

Hi all,
i'v been trying to understand how can i add more then one record to the
DB from a form in a single shot so to say..
I have an ITEM module, it has an ItemID and TYPEofItem , the TypeofItem
is been selected from a dropdown list that populates the item types i
have in another module *(called Types)..
I have everything working nicly and i am adding the ItemID with no
problem,
but! i am trying to understand if i can type in more then one ItemID
(with the same ItemType value) and submitting them all at once to the
DB..
Maby crating a list object (i have no idea on how to do that in the
view..) and filling out rows with differnt ID's and then running on the
list and adding them all...
Can someone please help me understand how to do that ? - thank's...

You want to use accepts_nested_attributes_for.

Best,

Hanan Y. wrote in post #960326:

Hi all,

i’v been trying to understand how can i add more then one record to the

DB from a form in a single shot so to say…

I have an ITEM module, it has an ItemID and TYPEofItem , the TypeofItem

is been selected from a dropdown list that populates the item types i

have in another module *(called Types)…

I have everything working nicly and i am adding the ItemID with no

problem,

but! i am trying to understand if i can type in more then one ItemID

(with the same ItemType value) and submitting them all at once to the

DB…

Maby crating a list object (i have no idea on how to do that in the

view…) and filling out rows with differnt ID’s and then running on the

list and adding them all…

Can someone please help me understand how to do that ? - thank’s…

You want to use accepts_nested_attributes_for.

I literally yesterday followed two railscasts which will get you up to speed with this:

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

Note that if you are using Rails 3 you will have to remove the “h(…)” in the helper method. If you run into trouble post back here as I just got this working and is pretty slick.

Wow, thanks guys !!!!!!!!!!!!!!! i dont have the time to check this out
right now but i will get on it and post back with some feedback :))

Hi all,

Thank you for the links, it seems to be what i am looking for although
it looks a big complex for me still ... :-\ hopefully i can understand
it so i can use it :slight_smile:

But, the tutorial was not what i need, it actually builds up a new
nested module module each time, i was hopping for using some sort of a
list (listbox) that i can add as many items (the UNIQ ID'S) and when i
submit it will loop though the list items(as many as present) and add
each to the db with the other attributes that are "fixed" in the form...
the reason i want to do this is because the id's are been inputted
together, and pressing "new" to create a new module will require to much
time to press new and then refilling the same information (type and
such) for each new nested module...

Is it possible to do that? :-Z

Thanks..

Hi all,

Thank you for the links, it seems to be what i am looking for although

it looks a big complex for me still … :-\ hopefully i can understand

it so i can use it :slight_smile:

But, the tutorial was not what i need, it actually builds up a new

nested module module each time, i was hopping for using some sort of a

list (listbox) that i can add as many items (the UNIQ ID’S) and when i

submit it will loop though the list items(as many as present) and add

each to the db with the other attributes that are “fixed” in the form…

the reason i want to do this is because the id’s are been inputted

together, and pressing “new” to create a new module will require to much

time to press new and then refilling the same information (type and

such) for each new nested module…

Sounds like perhaps the tutorial is exactly what you want, but need to implement using ajax…did you watch the second part of the tutorial - I think the first is 197, the next is 198 I think? Still sounds to me this is what you are trying to do. The second part implements ajax where you can add item after item and then submit all together.

Hi David, Thank your for the reply...
It is very similar to what i need but i want to not "create" a new
module in the form for each item rather have a listbox with as many row
as the user filled (like excl or something or take a textfield with
multiple known length ID and from submit these id's (after they where
broken to each id in an array or something) with the other "normal"
values..

So, i am actually looking for a way to make a listbox item that is like
excl (that you can just add text to the next row) or some sort of a
breakdown function for a textfield string and submitting them...

i will hope you will continue to reply to me because so far you have
been a grate help :slight_smile:

Hi all,
after allllooottt of research i found i could create a list box with
this code :

       <select name="itms[]" multiple="multiple" size="15">
        <option></option>

        <%= options_from_collection_for_select
@items,:imei,:type_name,$var %><br/><br/>
        </select>
        <br> </br>

This will create the list box i was craving for, and populate it with
the items values from the DB , my question is this..

1) How can i create this list box with no values and be able to add
values to it as needed
2) Once i have a listbox with the new values how can i loop though the
values and submit each one to the db as a new value ?

Thanks. :slight_smile:

Hi all,

after allllooottt of research i found i could create a list box with

this code :

   <select name="itms[]" multiple="multiple" size="15">

    <option></option>



    <%= options_from_collection_for_select

@items,:imei,:type_name,$var %>

    </select>

    <br> </br>

This will create the list box i was craving for, and populate it with

the items values from the DB , my question is this…

  1. How can i create this list box with no values and be able to add

values to it as needed

Someone else might have better idea, but from what I have seen you will need to create a text box and an add button, when you submit the add button use javascript to add the item to the list box. I think you will be adding both an index and the text displayed.

I still really dont understand why you dont want to do it the other way, again maybe I dont understand youir final outcome – but as far as I can understand the functionally on your page the result would be about the same, but this is what I would do if I needed to. Also note, that in my opinion you are beginning to fight Rails but trying to go outside and do something manually… in my experience I have always fared better by using conventions and existing functionality whenever possible, as I have found that when I do not, I end up coming upon special cases I did not think of and end up investing a lot more time (and usually end up going back and doing it the Rails way).

  1. Once i have a listbox with the new values how can i loop though the

values and submit each one to the db as a new value ?

You can request the params from the page in your controller.

So for example I think you could do something like (there are more concise ways and you will probably have to play around with it go get it to work – just turn on the debugger):

params[:itms].each do |itm|
item = Item.new
item.name = itm[:name]
item.save
end