Display integer through table in view
I have a table which contains 3 columns of data to be entered. I want to
add a fourth column that contains an integer. The integer will update each
time a new row is added.
For example, here's a table in my view:
_form.html.erb
<table>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<%= f.nested_fields_for :partial do |f|
render 'partial', f: f
end -%>
<caption align = "bottom"><b><u><%= link_to_add_fields "Add Field", f,
:manual_iops %></b></u></caption>
</table>
So I have this renders a partial with my table data. Here it is:
_partial.html.erb
<tr>
<td>
<%= f.text_field :data1 %>
</td>
<td>
<%= f.text_field :data2 %>
</td>
<td>
<%= f.text_field :data3 %>
</td>
</tr>
Every time I click link_to_add_fields it creates a new row in my table
with 3 columns. How can I create a 4th column with a simple integer that
updates every time I add a new row?
For example, my 4th column begins with integer x=5, then when I click
link_to_add_fields to add a second row, I add +10 to x, so now my 4th
column(row 2) is 15. Any ideas?
If any more information is needed, just let me know.
EDIT*
application.js
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
application_helper.rb
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index =>
"new_#{association}") do |builder|
render(association.to_s.singularize, :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\",
\"#{escape_javascript(fields)}\")")
end
No comments:
Post a Comment