i have implemented nested attribute form using nested attributes railscast guide. result, user can click icon dynamically add "child" rows view.
unfortunately, can make work last icon in view (illustrated here). icon generated in view, others generated in partial used render each row.
is possible this? if so, best approach?
here latest attempt.
sheet has_many
slots. in sheet edit view, use sheet form builder (sheet
) render slot partial , pass helper link_to_add_fields
renders link generate new row when clicked (this part works fine). you'll notice attempting pass sheet
partial can call link_to_add_fields
there breaks down:
the view - edit.html.haml
:
= sheet.fields_for :slots |builder| = render 'slots/edit_fields', f: builder, sheet:sheet = link_to_add_fields image_tag("plus.jpg", size:"18x18", alt:"plus"), sheet, :slots, 'slots/edit'
the partial - _edit_fields.html.haml
:
- random_id = securerandom.uuid .row.signup{:id => "edit-slot-#{random_id}"} .col-md-1 %span.plus-icon = link_to_add_fields image_tag("plus.jpg", size:"18x18", alt:"plus"), sheet, :slots, 'slots/edit' %span.minus-icon = image_tag "minus.jpg", size:"18x18", alt:"minus" .col-md-2= f.text_field :label ... other fields ...
the helper method:
def link_to_add_fields(name, f, association, partial) new_object = f.object.send(association).klass.new id = new_object.object_id fields = f.fields_for(association, new_object, child_index: id) |builder| render(partial.to_s.singularize + "_fields", f: builder, name: name) end link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) end
i undefined local variable or method 'sheet'
on call helper partial. basically, need sheet (parent) form builder available on each link helper work. or need give on approach , use ajax (also tried that).
update
after debugging bit, clear sheet
getting passed down partial. root issue seem setting endless recursion:
- partial invokes
link_to_add_fields
+
icon can serve "add child" link. link_to_add_fields
renders partial fields can generated when+
icon pressed.
the other issue running when original children rendered, sequential indexes in attribute collection (0, 1, 2,...). so, if figure out way render new child rows among originals, i'm not sure how able maintain order of children when form submitted without lot of jquery gymnastics or something.
undefined local variable or method 'sheet'
is being caused way rendering partial.
= render 'slots/edit_fields', f: builder, sheet:sheet
is not sufficient passing variables partial. need:
= render partial: 'slots/edit_fields', locals: {f: builder, sheet:sheet}
that make f available in partial.
Comments
Post a Comment