gopinath ponnivalavan


50 Experience
0 Lessons Completed
0 Questions Solved


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?

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.