I have the following (simplified) classes:
class CandidateList < ActiveRecord::Base
has_many :candidates, inverse_of: :candidate_list, dependent: :destroy
...
end
and
class Candidate < ActiveRecord::Base
belongs_to :candidate_list, inverse_of: :candidates
...
end
In my controller, I create a CandidateList
with 10 Candidate
objects and querying the database verifies that they exist and are attached.
In my form, I've got the following:
<%= simple_form_for save_bingo_terms_path( candidate_list_id: @candidate_list ) do |f| %>
...
<%= @candidate_list.candidates.count %>
<table>
<%= f.simple_fields_for :candidates do |cf| %>
<tr>
<td><%= cf.input :name, label: false %></td>
<td><%= cf.input :definition, label: false %></td>
</tr>
<% end %>
<% end %>
Can anyone help me understand why it only shows me a single row in the table with an input field for name
and definition
even though the count
output above the table shows 10
?
Thanks in advance!
:candidates
is not associated with the @candidate_list object, it's associated with a save_bingo_terms_path
Change the simple_form_for
to
<%= simple_form_for @candidate_list do |f| %>
And don't forget to specify...
class CandidateList < ActiveRecord::Base
has_many :candidates, inverse_of: :candidate_list, dependent: :destroy
accepts_nested_attributes_for :candidates
...
end
Alternatively you can explicitly specify an each
loop and reference each candidate
<% @candidate_list.candidates each do |candidate| %>
<% f.simple_fields_for :candidates, candidate do |cf| %>
<tr>
<td><%= cf.input :name, label: false %></td>
<td><%= cf.input :definition, label: false %></td>
</tr>
<% end %>
<% end %>