I have an edit page that generates a few input fields I organized in a table, like so: <tr> <td>Driver License</td> <td> <input id="person_accounts_attributes_0_account_documents_attributes_1_number" name="person[accounts_attributes][0][account_documents_attributes][1] [number]" type="text" /> </td> </tr> <tr> <td>Passport</td> <td> <input id="person_accounts_attributes_0_account_documents_attributes_47_number" name="person[accounts_attributes][0][account_documents_attributes][47] [number]" type="text" value="1" /> </td> </tr> <tr> <td>SSN</td> <td> <input id="person_accounts_attributes_0_account_documents_attributes_48_number" name="person[accounts_attributes][0][account_documents_attributes][48] [number]" type="text" value="2" /> </td> </tr> The same fields work great in a ''new'' page (probably because that page accepts values only once) but if I click on the Submit button of the ''edit'' page the fields that have a value generate duplicate records in the DB. The fields that contain no value do not. The ''update'' action just issues a @person.update_attributes(params[:person]) statement. I have been battling this for a long time. Any idea why this might be happening? Thank you. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Never mind. Got it working. I should pay more attention to the documentation. Sorry to waste your time. On Jul 20, 8:10 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> I have an edit page that generates a few input fields I organized in a > table, like so: > <tr> > <td>Driver License</td> > <td> > <input > id="person_accounts_attributes_0_account_documents_attributes_1_number" > name="person[accounts_attributes][0][account_documents_attributes][1] > [number]" type="text" /> > </td> > </tr> > > <tr> > <td>Passport</td> > <td> > <input > id="person_accounts_attributes_0_account_documents_attributes_47_number" > name="person[accounts_attributes][0][account_documents_attributes][47] > [number]" type="text" value="1" /> > </td> > </tr> > > <tr> > <td>SSN</td> > <td> > <input > id="person_accounts_attributes_0_account_documents_attributes_48_number" > name="person[accounts_attributes][0][account_documents_attributes][48] > [number]" type="text" value="2" /> > </td> > </tr> > > The same fields work great in a ''new'' page (probably because that page > accepts values only once) but if I click on the Submit button of the > ''edit'' page the fields that have a value generate duplicate records in > the DB. The fields that contain no value do not. > > The ''update'' action just issues a > @person.update_attributes(params[:person]) statement. > > I have been battling this for a long time. > > Any idea why this might be happening? > > Thank you.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 July 2010 01:10, pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote:> I have an edit page that generates a few input fields I organized in a > table, like so: > <tr> > <td>Driver License</td> > <td> > <input > id="person_accounts_attributes_0_account_documents_attributes_1_number" > name="person[accounts_attributes][0][account_documents_attributes][1] > [number]" type="text" /> > </td> > </tr> > > <tr> > <td>Passport</td> > <td> > <input > id="person_accounts_attributes_0_account_documents_attributes_47_number" > name="person[accounts_attributes][0][account_documents_attributes][47] > [number]" type="text" value="1" /> > </td> > </tr> > > <tr> > <td>SSN</td> > <td> > <input > id="person_accounts_attributes_0_account_documents_attributes_48_number" > name="person[accounts_attributes][0][account_documents_attributes][48] > [number]" type="text" value="2" /> > </td> > </tr> > > The same fields work great in a ''new'' page (probably because that page > accepts values only once) but if I click on the Submit button of the > ''edit'' page the fields that have a value generate duplicate records in > the DB. The fields that contain no value do not. > > The ''update'' action just issues a > @person.update_attributes(params[:person]) statement. > > I have been battling this for a long time.Have you had a look in development.log to get a clue to what is happening? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I did and it was not helpful in this case. The problem was that I was trying to duplicate manually what fields_for does because I needed to sort the output in a specific way (too long to explain here). Of course I was not doing a good job. I finally discovered (from a simpler use of fields_for that I have working) that fields_for generates actually 2 input tags for each one of the association records, one for the field you need and another one that is a hidden field and I believe keeps track of the ID value of the associated record (not very sure about that, though, I still have to check that out). Then, as usual, I thought that it was not possible that the framework didn''t have something to take care of my needs and hit the docs again for the nth time. I paid very close attention this time and sure enough I found an example that could work with a little bit of a hack to make my sorting work, and it did. On Jul 21, 3:42 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 21 July 2010 01:10, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: > > > > > I have an edit page that generates a few input fields I organized in a > > table, like so: > > <tr> > > <td>Driver License</td> > > <td> > > <input > > id="person_accounts_attributes_0_account_documents_attributes_1_number" > > name="person[accounts_attributes][0][account_documents_attributes][1] > > [number]" type="text" /> > > </td> > > </tr> > > > <tr> > > <td>Passport</td> > > <td> > > <input > > id="person_accounts_attributes_0_account_documents_attributes_47_number" > > name="person[accounts_attributes][0][account_documents_attributes][47] > > [number]" type="text" value="1" /> > > </td> > > </tr> > > > <tr> > > <td>SSN</td> > > <td> > > <input > > id="person_accounts_attributes_0_account_documents_attributes_48_number" > > name="person[accounts_attributes][0][account_documents_attributes][48] > > [number]" type="text" value="2" /> > > </td> > > </tr> > > > The same fields work great in a ''new'' page (probably because that page > > accepts values only once) but if I click on the Submit button of the > > ''edit'' page the fields that have a value generate duplicate records in > > the DB. The fields that contain no value do not. > > > The ''update'' action just issues a > > @person.update_attributes(params[:person]) statement. > > > I have been battling this for a long time. > > Have you had a look in development.log to get a clue to what is happening? > > Colin-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 22, 5:22 am, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> [...] I finally discovered (from a > simpler use of fields_for that I have working) that fields_for > generates actually 2 input tags for each one of the association > records, one for the field you need and another one that is a hidden > field and I believe keeps track of the ID value of the associated > record (not very sure about that, though, I still have to check that > out).Just a comment. It turns out the hidden fields I saw in the code I had already working were being generated because the fields being used were check boxes and Rails needs them because of the particular behavior of check boxes. The final thing only generates one field per text_field, no hidden fields. In any case, the problem is finally fixed and I have learned quite a few things in the process of working with fields_for. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.