Skip to main content

How do I display dynamic fields from collection and bind data back?

Rails • Asked by gopinath ponnivalavan
I just started working on Ruby on Rails project for maintenance.  I have array of objects and want to display separate section of fields for each object.  They have design to call field factory that generate field dynamically.  I could display the data but it not binding the value back on page submission

ClassRoom Controller

def arrayOfStudents
  @arrayOfStudents||= begin
    if Array(attributes[:arrayOfStudents]).any?
      attributes[:arrayOfStudents]   ==> this always have 5 objects of Students with variables name and age

My view

<% @classroom.arrayOfStudents.each do |crt| %>
  <%= f.fields_for crt do |ff| %>
    <%= render 'shared/model_attributes/field', f: ff, field: {type: :money, cols: 6, name: :name, label: 'Name'}, static: static %>
    <%= render 'shared/model_attributes/field', f: ff, field: {type: :percent, cols: 6, name: :age, label: 'Age'}, static: static %>
    <%= puts "testing"%>
  <% end %>
<% end %>

attributes/field

<% when :money %>
    <%= f.labeled_text_field_with_begin_addon(field[:name], '$', label: field[:label], class: class_list, placeholder: field[:label], maxlength: field[:maxlength], display_name: false, disabled: false, reserve_error_space: true, help_text: field[:help_text]) %>

After editing the value and submit control comes to controller.  But the values inside :arrayOfStudents are unchanged .   Am I missing generating dynamic fields correctly?




Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.