active_interaction icon indicating copy to clipboard operation
active_interaction copied to clipboard

Nested forms

Open mbajur opened this issue 1 year ago • 4 comments

Hello, I'm trying to use ActiveInteraction with nested forms feature of rails. Here is my view:

<%= form_with model: interaction do |f| %>
  <%= f.fields_for :sections do |ff| %>
    <%= debug ff.object.class %>
  <% end %>
<% end %>

For some reason, the debug line is not returning a nested section record but nil. On the other hand, if i pass a second argument to fields_for (collection) as, say, f.fields_for :sections, record.sections, the debug line returns ActiveRecord::Relation instance instead of the record.

Changing this code to use a direct record instead of interaction objects fixes everything and debug starts returning actual record objects which means that there is something wrong about ActiveInteraction that yelds a collection to fields_for block instead of iterating through objects. Or i just did something wrong :)

Thanks in advance for any clues!

mbajur avatar Dec 24 '23 07:12 mbajur

Is this an issue with the code itself, or a general question? For general questions Stackoverflow or Github Discussions (if one is setup for this repo) is better than Github.

sandstrom avatar Mar 26 '24 14:03 sandstrom

It's happening only when using active_interaction so it seems like a bug with AI, not a general question

mbajur avatar Mar 26 '24 19:03 mbajur

Or i just did something wrong :)

Yes, you are correct, that's what I observe too. I didn't get to the bottom of it, so I'm just always passing the second argument.

antulik avatar Apr 23 '24 01:04 antulik

Update. fields_foris looking for a specific interface on the object which is implemented by accepts_nested_attributes_for in ActiveRecord::NestedAttributes::ClassMethods.

Specifically methods

  • #{association}_attributes= and
  • association

I've started implementing accepts_nested_attributes_for extension for active_interaction. It's still experimental, and only implemented for collections, but I already found it a great time saver for implementing nested forms.

You can see it here. https://github.com/antulik/active_interaction-extras/blob/master/lib/active_interaction/extras/nested_attributes.rb 

antulik avatar Aug 08 '24 22:08 antulik