Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested attributes / nested form and draper followup #92

Closed
tmaier opened this issue Dec 5, 2011 · 4 comments
Closed

nested attributes / nested form and draper followup #92

tmaier opened this issue Dec 5, 2011 · 4 comments

Comments

@tmaier
Copy link
Contributor

tmaier commented Dec 5, 2011

I have an issue which is related to #52.

I use in my forms Ryan Bates' nested_form gem. It allows to add and remove new fields with JavaScript.
When I start to use draper, the generated html changes.
Namely the name hash of the field misses one level.

Before draper:

<div class="input string tel optional">
  <label class="tel optional" for="organization_phones_attributes_new_1323093036345_phone_number"> Phone number</label>
  <input class="string tel optional" id="organization_phones_attributes_new_1323093036345_phone_number" name="organization[phones_attributes][new_1323093036345][phone_number]" placeholder="e.g. +49 49 3423232" size="50" type="tel">
  <span class="hint">Has to start with international code, e.g.+49 49 3423232</span>
</div>

Submit results in the following parameters:

  Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"qbyOwm7VbFv2MblbKjNyf7HwGGZ97/fHHi6RCI4VDok=",
"organization"=>{
  "name"=>"Example Co. 0",
  "phones_attributes"=>{
    "0"=>{
      "phone_number"=>"49893246240",
      "_destroy"=>"false",
      "id"=>"4eda6313499dda40f700000b"},
    "new_1323093036345"=>{
      "phone_number"=>"+30837748374",
      "_destroy"=>"false"
    }
  }
},
"commit"=>"Update Organization",
"id"=>"4ec580a7499dda4880000006"}

phones_attributes[0] is an existing and already saved record. The HTML for this one is also correct when using draper.
phones_attributes[new_1323093036345] is the new record, created by clicking "Add phone number" and the related JS.

With draper, I get the following HTML.

<div class="input string tel optional">
  <label class="tel optional" for="organization_phones_attributes_phone_number"> Phone number</label>
  <input class="string tel optional" id="organization_phones_attributes_phone_number" name="organization[phones_attributes][phone_number]" placeholder="e.g. +49 49 3423232" size="50" type="tel">
  <span class="hint">Has to start with international code, e.g. +49 49 3423232</span>
</div>

Submit results in the following parameters:

  Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"qbyOwm7VbFv2MblbKjNyf7HwGGZ97/fHHi6RCI4VDok=",
"organization"=>{
  "name"=>"Example Co. 0",
  "phones_attributes"=>{
    "0"=>{
      "phone_number"=>"49893246240",
      "_destroy"=>"false",
      "id"=>"4eda6313499dda40f700000b"
    },
  "phone_number"=>"+30837748374",
  "_destroy"=>"false"}
},
"commit"=>"Update Organization",
"id"=>"4ec580a7499dda4880000006"}

Current findings

I think nested_form calls the model attributes at https://github.com/ryanb/nested_form/blob/master/lib/nested_form/builder_mixin.rb in a way how draper does not like it.

@tmaier
Copy link
Contributor Author

tmaier commented Dec 6, 2011

It seems like this bug is not related to nested_form. It's related to ActionView.

When using draper, fields_for does not set the :child_index (as called by nested_form in line 25).

FormHelper uses and sets the child_index in https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb#L1398-1415

I added some debug calls for the method fields_for_with_nested_attributes and this is the result:

start fields_for_with_nested_attributes for new_phones
  respnds to persisted
    before line 1403: #<Phone:0x000001026e9120>
    after line 1403:  #<Phone:0x000001026e9120>
  respond_to?(:to_ary)
    nay!
start fields_for_with_nested_attributes for new_emails
  respnds to persisted
    before line 1403: #<Email:0x00000102539ac8>
    after line 1403:  [#<Email _id: 4ede6477499dda1d14000006, _type: nil, type: "Work", email: nil, status: nil>]
  respond_to?(:to_ary)
    yay!

phones and emails have exactly the same relation (mongoid, embeds_many), but to different Models. phones uses PhoneDecorator and emails Email.

This means the call to

association = [association] if @object.send(association_name).is_a?(Array)

in line 1403 fails.

@steveklabnik
Copy link
Member

Is this still an issue now that that pull request has been merged in?

@tmaier
Copy link
Contributor Author

tmaier commented Jan 5, 2012

No. Thanks for merging!

@tmaier tmaier closed this as completed Jan 5, 2012
@steveklabnik
Copy link
Member

w00t!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants