0

Got an Issue,

I've got a Multitabbed Form. These Tabs can be created and deleted(Via JS) by the User. All these Tabs contain the exact same Fields all belonging to aparticular Entity. (As an Instance,Various RateSlots "belonging" to a RateTag)

My Issue is how to submit the details on all tabs.

In My current Implementation, i dynamically assign new IDs to each form fields in the various tabs, loop through them, Add all values to an Array and make an API call,passing RateSlots for a RateTag..

I somehow think this is inefficient, Please is there a better Approach.

Looking at having the Fields in all the Tabs having the Same ID instead of multiple different ids.

There isn't any Backing Model as an API call is made with the details on form Submit.

Any Help would be highly appreciated

enter image description here

dev
  • 382
  • 1
  • 3
  • 17

1 Answers1

0

Not sure if it's much connected with Rails, seems it's mostly JavaScript-related, is it?

You can check RailsCast #197 episode to get an idea.

What he does is creating a "template" set of fields, using known string key inside elements names and ids. code

fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
  render(association.to_s.singularize + "_fields", :f => builder)
end

Where association is ~ name of child object, e.g. "new_rateslot". Then, for adding a set of fields in JavaScript, it creates a copy and substitute this known string ("new_rateslot") for generated unique ID. code

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).up().insert({
    before: content.replace(regexp, new_id)
  });
}

So it creates new fields which would look like

input id="ratetag_rateslot_attributes_unique_id" name="ratetag[rateslot_attributes][unique_id]"

Thus instead of generating completely random IDs, it uses meaningful structure like parent_object_name[child_object_name][child_field_name][unique_id] or something

Check this answer for some explanation of the idea

Community
  • 1
  • 1
Pavel Bulanov
  • 933
  • 6
  • 13