EDIT - fixed: The problem was the params hash and after looking through a lot of questions on SO, i came up with the following solution, which (so far) works well:
the student_groups#new
action now looks like this - maybe there's a better way to parse the params, but ...this works!
def create
@student_group = @user.student_groups.build(params[:student_group])
### https://stackoverflow.com/questions/2610263/how-to-access-nested-params
@params = params[:student_group][:students_attributes]
if @student_group.save
### https://stackoverflow.com/questions/14502508/undefined-method-for-nilnilclass-when-pushing-values-to-an-array
if @params.present?
@params.values.each do |student|
@student_group.students.create(name:"#{student[:name]}", gender: "#{student[:gender]}")
end
end
# new subject path
redirect_to class_path(@student_group), flash: { success: "#{@student_group.name} has been added successfully" }
else
@title = "Create a new group"
flash.now[:error] = "Something's gone wrong. Please try again!"
render 'new'
end
end
student_group.rb
: I had to mark :_destroy
as attr_accessor
class StudentGroup < ActiveRecord::Base
### https://github.com/ryanb/nested_form/issues/222
attr_accessor :_destroy
attr_accessible :name, :number_of_students, :type_of_group, :students_attributes
belongs_to :user
has_one :age, dependent: :destroy
has_many :students, dependent: :destroy
accepts_nested_attributes_for :students, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
...
end
and _groups_form.html.erb
: Added child_index as per this
<%= form_for @student_group do |f| %>
<p>
The name of this group is
<span class="field form_field"><%= f.text_field :name %></span>
and it is a/an
<span class="field dropdown"><%= f.select :type_of_group, [["select a group type", ""], "young learners class (0-6)", "primary class (7-12)", "secondary class (13-17)", "adult class (18+)", "children's sport team", "adult's sport team"] %></span>
<!-- <span id="nos" class="field dropdown"><%#= f.select :number_of_students, (0..60) %></span> -->
</p>
<table id="nos_header">
<thead>
<tr>
<th>Student name:</th>
<th>Gender:</th>
</tr>
</thead>
<tbody>
<!-- https://stackoverflow.com/questions/11445831/how-to-submit-multiple-new-items-via-rails-3-2-mass-assignment -->
<%= f.fields_for :students, @student, child_index: @student do |builder| %>
<%= render "student_fields", :f => builder %>
<% end %>
<tr>
<td class="links"><%= link_to_add_association 'add student', f, :students %></td>
</tr>
</tbody>
</table>
<%= f.submit "Submit", :class => 'big_button round unselectable'%>
<% end %>
END EDIT
I'm using rails 3.2.13
and cocoon
to make a nested form in my student_group model. The formatting is a still a bit screwy, and i'd like to implement coffeescript to auto update the correct number of students, but these are things i can figure out later. For now, the main problem is that while the forms show up and can be filled in/submitted without errors, new children models are not being created.
here's the _form
partial
<%= form_for @student_group do |f| %>
<p>
The name of this group is
<span class="field form_field"><%= f.text_field :name %></span>
and it is a/an
<span class="field dropdown"><%= f.select :type_of_group, [["select a group type", ""], "young learners class (0-6)", "primary class (7-12)", "secondary class (13-17)", "adult class (18+)", "children's sport team", "adult's sport team"] %></span>
<!-- <span id="nos" class="field dropdown"><%#= f.select :number_of_students, (0..60) %></span> -->
</p>
<table id="nos_header">
<thead>
<tr>
<th>Student name:</th>
<th>Gender:</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :students do |builder| %>
<%= render "student_fields", :f => builder %>
<% end %>
<td class="links"><%= link_to_add_association 'add student', f, :students %></td>
</tbody>
</table>
<%= f.submit "Submit", :class => 'big_button round unselectable'%>
<% end %>
and the _student_fields
partial:
<tr class="nested-fields">
<td class="field form_field"><%= f.text_field :name %></td>
<td class="field dropdown"><%= f.select :gender, [["select a gender", ""],'Female', 'Male', 'Transgender'] %></td>
<td><%= link_to_remove_association "remove student", f %></td>
</tr>
When I make a new group in the browser, the new students are not created and I can see this in the server:
Started POST "/student_groups" for 127.0.0.1 at 2013-07-04 14:58:51 +0200
Processing by StudentGroupsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"u/DbVbgMKoT6kWPBKsD0sVNBLVRpFY87E5nZQWK+K9o=", "student_group"=>{"name"=>"test", "type_of_group"=>"young learners class (0-6)", "students_attributes"=>{"1372943007652"=>{"name"=>"Johnny Test", "gender"=>"Male", "_destroy"=>""}, "1372943009403"=>{"name"=>"Quizzy", "gender"=>"Transgender", "_destroy"=>""}}}, "commit"=>"Submit"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 56 LIMIT 1
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "student_groups" ("created_at", "name", "number_of_students", "type_of_group", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Thu, 04 Jul 2013 12:58:51 UTC +00:00], ["name", "Test"], ["number_of_students", nil], ["type_of_group", "adult class (18+)"], ["updated_at", Thu, 04 Jul 2013 12:58:51 UTC +00:00], ["user_id", 56]]
(2.4ms) commit transaction
Redirected to http://localhost:3000/class/407
Completed 302 Found in 8ms (ActiveRecord: 3.3ms)
It's this hash that seems to be the issue -
"students_attributes"=>{"1372943007652"=>{"name"=>"Johnny Test", "gender"=>"Male", "_destroy"=>""}, "1372943009403"=>{"name"=>"Quizzy", "gender"=>"Transgender", "_destroy"=>""}}
where should these student attributes go/how should i deal with them so that they create a new student(s)? Thanks!