Ask A Question


You’re not receiving notifications from this thread.

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

gopinath ponnivalavan asked in Rails
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 %>


<% 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?

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,969+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.