Prefill a simple_field_for with external datas

19 views
Skip to first unread message

Pierre Olivier Tran

unread,
Nov 18, 2016, 4:09:39 AM11/18/16
to SimpleForm

Is it possible to prefill a simple_fields_for ?

I have a User, who has_one backpack, which has_may pockets, each pocket has_many items.

I created a default backpack, and I would like to prefill my user form backpack fields with the datas from this default backpack.

I tried everything I could think of:

1 - In my controller

@default_backpack = Backpack.first
@backpack = @default_backpack.deep_clone include: [ :pockets, { pockets: :items } ]
@backpack.save
@user.backpack = @backpack

But this leads me to a Couldn't find Backpack with ID=597 for USer with ID=

2- In my view

<%= f.simple_fields_for :ibackpack, @default_backpackdo |bkpk| %>
<%= render 'backpacks/form', f: bkpk %>
<% end %>

Which leads me to empty backpack fields. I start my form with a simple

      <%= simple_form_for @user do |f| %>
Now, the funny part ! If I add a simple <%= @default_backpack.pockets.size %>, it returns 1, and <%= @default_backpack.pockets.first.items.size %> returns 3, 
which basically means that the item IS cloned, but the fields are not pre-filled with it.
Any idea of a proper way to pre-fill them ?

Walter Lee Davis

unread,
Nov 18, 2016, 7:43:55 AM11/18/16
to plataformate...@googlegroups.com

> On Nov 18, 2016, at 4:09 AM, Pierre Olivier Tran <firz...@gmail.com> wrote:
>
> Is it possible to prefill a simple_fields_for ?
>
> I have a User, who has_one backpack, which has_may pockets, each pocket has_many items.
>
> I created a default backpack, and I would like to prefill my user form backpack fields with the datas from this default backpack.
>
> I tried everything I could think of:
>
> 1 - In my controller
>
> @default_backpack = Backpack
> .first
>
> @backpack = @default_backpack.deep_clone include: [ :pockets, { pockets: :items
> } ]
>
> @backpack
> .save
>
> @user.backpack = @backpack
> But this leads me to a Couldn't find Backpack with ID=597 for USer with ID=
>

I think you may be really close here: try setting the whole thing into memory:

@user.backpack ||= Backpack.first.deep_clone(include: [:pockets, :items])

and don't save any part of it before you show the form. Just leave it in the association for the form (and associations, and accepts_nested_attributes_for) to complete when you save the @user.

I'm not clear what your associations look like for the backpack, so that include bit I wrote may be wrong -- what you have here looks unlike anything I have seen.

If this works, then you may also want to go back and set inverse_of everywhere in your associations, because you're probably seeing the issue where you have more than one instance of a given side of an association present at the same time, and your attributes are being set in the wrong place.

Walter

> 2- In my view
>
> <%= f.simple_fields_for :ibackpack, @default_backpackdo |bkpk| %>
> <%= render 'backpacks/form', f: bkpk %>
> <% end %>
>
> Which leads me to empty backpack fields. I start my form with a simple
>
> <%= simple_form_for @user do |f| %>
> Now, the funny part ! If I add a simple <%= @default_backpack.pockets.size %>, it returns 1, and <%= @default_backpack.pockets.first.items.size %> returns 3,
> which basically means that the item IS cloned, but the fields are not pre-filled with it.
> Any idea of a proper way to pre-fill them ?
>
> --
> You received this message because you are subscribed to the Google Groups "SimpleForm" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages