I’m new in rails and need some help creating a Nested form.
I have this in teams/_form.html.erb
<%= form_for @team do |f| %>
<div class="field">
<%= f.label "Name" %><br />
<%= f.text_field :name, :required => true %>
</div>
<%= f.fields_for :players do |builder| %>
<%= render :partial => 'players_field', :f => builder %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What I want is create a list of players in the team form. The problem is that the render don’t work and players_field.html.erb don’t render.
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Position</th>
</tr>
</thead>
<tbody id="tableRow">
<tr>
</tr>
</tbody>
</table>
TR is added to the table with js.
UPDATE:
Another problem appear now :/
This is my js:
var newRow = document.createElement('tr');
newRow.innerHTML = "<td>"+ counter +"</td>"+
"<%= f.text_field :name %>"+
"<%= f.text_field :position %>"+;
document.getElementById("tableRow").appendChild(newRow);
Error:
undefined local variable or method `f' for #<#<Class:0x0000000288dd38>:0x007f77cc1226e0>
Why I can’t add this ?
The block
is basically a loop through
@team.players. It might be the case that@teamsimply doesn’t have any players to loop through. To show the form for at least one player you can change this to