nested_form_fields
nested_form_fields copied to clipboard
Cannot access attributes of nested object
When trying to gain access to the attributes of an associated object whilst updating its parent, it is possible to access the associated object, but not its attributes. For instance, the following works:
<%= f.nested_fields_for :charges do |cf| %>
<%= cf.object.account %>
<% end %>
Whereas this fails with undefined method 'id' for nil:NilClass
:
<%= f.nested_fields_for :charges do |cf| %>
<%= cf.object.account.id %>
<% end %>
Laws of Demeter aside, this feels profoundly odd.
It does indeed :) what does cf.object give you exactly? A Charge instance would be expected.
Yes, it gives a Charge instance. And the correct one too! It's as though Rails doesn't go to the database to find associated objects if they haven't been loaded.
I have never seen this behavior. most likely it has nothing to do with nested_form_fields, as nested_form_fields simply sits on top of the basic rails form builder. Can you try to remove nested_form_fields and see if the issue is still there?
I can confirm that the issue is not there if I use fields_for
.
oh, that is really odd... i need to check if nested_form_fields does anything different here than fields_for... strange, i didn't expect that
oops, sorry for the unrelated (now deleted) comment, i accidentally replied to the wrong email ;-)
On Wed, Dec 23, 2015 at 10:43 PM, Nico Ritsche [email protected] wrote:
they are blocked now :) they wont get through to the app, just see a "forbidden" page
On Wed, Dec 23, 2015 at 10:17 PM, wjdhamilton [email protected] wrote:
I can confirm that the issue is not there if I use fields_for.
— Reply to this email directly or view it on GitHub https://github.com/ncri/nested_form_fields/issues/55#issuecomment-166983389 .
LOL I was struggling to put that one into context!
happened to me the first time ;-)
Well, at least for now you can use a workaround:
- account_id = cf.object.account_id
or, if you need the whole account object:
- account = Account.find(cf.object.account_id)
I swear I tried the first option without success but it's working now! Thanks for your help and attention :+1:
I'm currently facing the same problem. It only works on 1 parent model association, the other two, no luck:
<%= form.nested_fields_for :delivery_items, wrapper_tag: :tr do |ff| %>
<td class="col-md-1">
<div class="input-product_code-div">
<%= ff.object.product.name %> <br> #works!
<%= ff.object.sales_order_item.address %> <br> #does not work
<%= ff.object.delivery.id %> #does not work
<%end%>
</div>
</td>
<% end %>
@bassman88: does the workaround work for you?