2

I',m trying to achieve a simple nested model form in Rails 4. This is probably something very simple, but just can't figure it out.

So I have an edit view, in which I create the nested fields:

    <%= f.fields_for :bankaccounts do |bankaccount| %>
     <%= render 'bankaccount_fields', :f => bankaccount %>
    <% end %>

These work fine and even the link:

    <%= link_to_add_association 'add', f, :bankaccounts , :class => "link"%>

works fine. The fields appear as they should when clicked.

The real problem is, that when I submit the form. It doesn't send the parameters back to web server which have been inputted into these added fields. (Can't see them anywhere in the webrick console) The form only submits the old bankaccount parameters as it should:

    "bankaccounts_attributes"=>{"0"=>{"iban"=>"121221", "swift"=>"121212", "id"=>"3"}, "1"=>{"iban"=>"33333", "swift"=>"33333", "id"=>"9"}, "2"=>{"iban"=>"121212", "swift"=>"211212", "id"=>"10"} 

Above attributes should contain four bank accounts.

FWIW: Turbolinks is also disabled.

EDIT: Here's the code from bankaccount_fields partial:

 <div id="tilit">
    <div class="area-form">
       <div class="col">
          <div class="box-row">
            <label for="iban" class="label">Iban</label>
            <div class="input-row no-italic">
              <%= f.text_field :iban, :id => "iban", :class => "input-text" %>
            </div>
          </div>
        </div>
    <div class="col">
      <div class="box-row">
         <label for="swift" class="label">Swift</label>
         <div class="input-row no-italic">
           <%= f.text_field :swift, :id => "swift", :class => "input-text" %>
         </div>
      </div>
    </div>
 </div>

pic of inspect element

EDIT: Debugger outputs reg. parameters:

(rdb:2) @_params {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"181xsndllcPmpvpMaDi70IXh8SpaSYxs0eiSo19qwfI=", "producer"=>{"name"=>"Anne", "RH_nro"=>"", "ytunnus"=>"", "prepaymentpercentage"=>"", "streetaddress"=>"", "zip"=>"", "city"=>"", "www"=>"", "bankaccounts_attributes"=>{"0"=>{"iban"=>"13123123", "swift"=>"121212", "id"=>"3"}, "1"=>{"iban"=>"33333", "swift"=>"33333", "id"=>"9"}, "2"=>{"iban"=>"121212", "swift"=>"211212", "id"=>"10"}}}, "commit"=>"TALLENNA", "action"=>"update", "controller"=>"producers", "id"=>"54"}

Should contain one more bankaccount_attributes record, but only contains the ones which are not dynamically added.

I've been scratching my head for hours now, so any help would be greatly appreciated.

Also sorry if the question is formed badly, this is my first :)

7
  • Can you include your link_to_add_association method and also the code that inserts the fields into your form? When a new input field is inserted, if you inspect it in the browser, what is its element id? Commented Oct 12, 2013 at 14:45
  • Should have specified that the link_to_add_association comes in this particular example from the cocoon gem. But the same problem persists when using nested_forms gem or the railscasts #196 nested model example. Added a screenshot from browser source code view. Commented Oct 12, 2013 at 17:15
  • That looks OK for getting the values down to the server. You can check to see if the parameters are being passed down if you use the debugger gem and temporarily add a debugger statement in your controller action. When you reach the breakpoint you can look at what is in params[:producer][:bankaccounts_attributes]. If the right values appear there, then check out the suggestions from @Rich Peck below. Commented Oct 12, 2013 at 19:00
  • Added the output from debugger. It really seems that the parameters won't get submitted to the server as I originally thought. Commented Oct 15, 2013 at 6:55
  • When your JavaScript inserts the new input field are they contained inside the <form> element or are they outside of it? Commented Oct 15, 2013 at 9:53

2 Answers 2

1

I have found the problem to my application, If don't know if anyone find this useful anymore or not.

I am guessing you have written your form_for inside a HTML table(that's what I did). If so the browser generates a inconsistent DOM. If you generate form inputs dynamically it wont put it inside the form unless you are selecting it through form selector. So the solution is put the form outside the HTML table.

Sign up to request clarification or add additional context in comments.

1 Comment

I also ran into this with rails .and I wasted about 5+ hours on it. I was using jQuery for DOM manipulation - So I will have to say its' a jQuery issue rather than browser or rails.
0

What are your strong params & controller like?

They should be like this for nested attributes:

def params
    params.require(:form_for_variable).permit(bankaccounts_attributes: [:iban, :swift])
end

Also, you'll need to include accepts_nested_attributes_for :bankaccounts in the model you're submitting the form to

3 Comments

You may want to include :id as well or else it can create new records when you meant to update existing ones: params.require(:producer).permit(bankaccounts_attributes: [:id, :iban, :swift]).
The nested parameters are all fine, even got the :id in there. :) If I submit with a static field, the form works fine. The problem here is that the dynamically created field's contents won't get submitted to the server.
I guess this "include the :id" is the real answer to this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.