I have a nested form that I am working on, that adds products to a job sheet.
At the moment It starts with 7 rows by doing 7.times { @job_sheet.products.new }
in the new action.
I would like It so It starts with one but create an add button that can add more rows, and a remove to remove them.
I found this RailsCast which is exactly what I want to do.
However I have no idea what this is doing
module ApplicationHelper
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
end
Can somebody please break this down for me so I can understand it, and is there a better or easier way of doing this?
Thanks
NOTE The code would not format correctly but it is at the bottom of the link I have provided.