ruby on rails - Prefill nested model fields with simple_form -
is possible prefill simple_fields_for
?
i have users
, has_one backpack
, has_may pockets
, each pocket
has_many items
.
i created default backpack, , prefill user form backpack fields datas default backpack.
i tried think of:
1 - in controller
@default_backpack = backpack.first @backpack = @default_backpack.deep_clone include: [ :pockets, { pockets: :items } ] @backpack.save @user.backpack = @backpack
but leads me couldn't find backpack id=597 user id=
2- in view
<%= f.simple_fields_for :ibackpack, @default_backpackdo |bkpk| %> <%= render 'backpacks/form', f: bkpk %> <% end %>
which leads me weird behaviour: backpack fields pre-filled, child fields (pockets
, items
) empty. start form simple
<%= simple_form_for @user |f| %>
here's simple_fields_for
<%= f.simple_fields_for :invoice_quote_element, @invoice_quote_element |iqe| %> <%= render 'briefs/quote_proposal', f: iqe %> <% end %>
along of partials used:
backpack
<div class="col-xs-12"> <%= f.input :rate, placeholder: t('simple_form.placeholders.backpack.rate') %> <div id="pockets"> <%= f.object.pockets.size %> <%= f.simple_fields_for :pockets, f.object.pockets.order(:position) |pocket| %> <%= render 'users/pocket_fields', f: pocket %> <% end %> </div> <div class="row"></div> <%= link_to_add_association t('simple_form.buttons.add_pocket'), f, :pockets, partial: 'users/pocket_fields', class: 'add-pocket-link futura-info padding green' %> </div>
pockets
<div class="col-xs-12 categories-and-items"> <div class="category form-inline" id="category_<%= f.object.id %>"> <%= f.hidden_field :position %> <div class="form-group col-xs-6"> <%= fa_icon "arrows", class:"blue" %> <% if current_user.admin? || f.object.user_id.nil? || f.object.user_id == current_user.id %> <%= f.input :title, label: t('simple_form.labels.pocket.title'), label: false, class: 'text-right', placeholder: t('simple_form.placeholders.pocket.title'), input_html: { class: 'backpack-category-title form-text form-b form-b-all col-xs-12' } %> <% else %> <%= f.input :title, label: t('simple_form.labels.pocket.title'), label: false, class: 'text-right', placeholder: t('simple_form.placeholders.pocket.title'), input_html: { class: 'backpack-category-title form-text form-b form-b-all col-xs-12', readonly: true } %> <% end %> </div> <% if current_user.admin? || (f.object.user_id.nil? || f.object.user_id == current_user.id) %> <div class="form-group col-xs-6"> <%= link_to_remove_association(t('simple_form.buttons.remove_pocket'), f, { wrapper_class: 'category', class: 'futura-info futura-info-small red remove-item-category' }) %> </div> <% end %> <div class="row"></div> <div class="col-xs-12 padding-0-2"> <hr> </div> <div class="items"> <%= f.object.items.size %> <%= f.simple_fields_for :items, f.object.items.order(:position) |item| %> <%= render 'users/item_fields', f: item %> <% end %> </div> <div class="item-link"> <%= link_to_add_association t('simple_form.buttons.add_item'), f, :items, partial: 'users/item_fields', render_options: { wrapper: 'item' }, class: 'add-item-link futura-info padding green' %> </div> </div> </div>
and items
<br /> <div class="col-xs-12 padding-0-2"> <div class="item form-inline" id="item_<%= f.object.id %>"> <div class="form-group col-xs-6 item-field"> <%= fa_icon "arrows" %> <% if current_user.admin? || f.object.user_id.nil? || f.object.user_id == current_user.id %> <%= f.input :title, label: false, required: true, placeholder: t('simple_form.placeholders.item.title'), input_html: { class: 'backpack-item-title form-text form-b form-b-all' } %> <% else %> <%= f.input :title, label: false, required: true, placeholder: t('simple_form.placeholders.item.title'), input_html: { class: 'backpack-item-title form-text form-b form-b-all', :readonly => true } %> <% end %> <% if current_user.admin? || f.object.user_id.nil? || f.object.user_id == current_user.id %> <%= f.input :day_based, input_html: { class: 'backpack-checkbox-day-based', placeholder: t('simple_form.placeholders.item.day_based') } %> <% else %> <%= f.input :day_based, input_html: { class: 'backpack-checkbox-day-based', placeholder: t('simple_form.placeholders.item.day_based'), disabled: true } %> <% end %> </div> <div class="form-group col-xs-3"> <%= f.input :days, label: false, required: true, input_html: { class: 'backpack-item-days form-text form-b form-b-all', placeholder: t('simple_form.placeholders.item.days') } %> </div> <div class="form-group col-xs-3"> <%= f.input :price, label: false, required: true, input_html: { class: 'backpack-item-price item-price-input form-text form-b form-b-all', placeholder: t('simple_form.placeholders.item.price') } %> </div> <div class="row"></div> <% if current_user.admin? || (f.object.user_id.nil? || f.object.user_id == current_user.id) %> <div class="form-group"> <%= link_to_remove_association(t('simple_form.buttons.remove_item'), f, { wrapper_class: 'item', class: 'futura-info futura-info-small red remove-item' }) %> </div> <% end %> </div> </div>
Comments
Post a Comment