Closed Kutomore closed 5 years ago
This is a valid question, it is not a new problem, it is described in the documentation, but I can understand it is maybe not immediately clear when reading that there is an option that could help you.
But in short: the link_to_add_association
builds the object to render the form, and in most default cases this is enough. But for cases like this, cocoon has the option :wrap_object to add a specific initialisation.
So in your case you would have to do something like:
<%=link_to_add_association 'Add Frame',
form,
:frames,
class: 'button btn btn-primary col-md-12',
partial: 'frames_form',
form_name: 'frames_form',
wrap_object: Proc.new { |frame| frame.build_image; frame }
%>
Works flawlesly, thanks man.
Hi, I'm the same guy from #556 experiencing a different issue. Right now everything is working as expected, except that when I add a new Frame, it doesn't build said frame's Image, for example, on the controller I build the Frame like
@order.frames.build.build_image
and then when I call my form:Which in turn renders:
Which, once I click the "Add Image" button, opens a modal with the following image form:
This works for the first frame, which has an instantiated image. But when it comes to the frames that Cocoon adds it doesn't instantiate a new Image for the Frame, so when I try to open the modal all I see is: An empty modal body. I assume it is empty because rails tries to create
fields_for :image
, and since:image
is nil it doesn't render anything.If I build it on the controler before, or if, like an edit, the frame already exists it works fine.
So I assume I have to instantiate the Image manually, how would I go about solving this?