I am trying to implement a checkbox in a nested form.
I have 3 models: Subjects, Lessons and Groups.
A Subjects has many lessons and each lesson has and belongs to many groups.
To put it in another way:
I have Subjects as the parent class, Lessons as the child class and Groups as the “grandchild” class.
In the Subjects form, I have allowed the creation of 3 lessons in a nested form.
I have already created groups before hand and right now, I want to create HABTM checkboxes for groups in each lesson but I am facing some trouble trying to send the parameters in Rails.
The main difference in the parameters is that currently I am sending
"lesson"=>{"group_ids"=>["105", "106", "107"]}
on its own. However what I want is for the group_ids to be sent for each individual lessons of the subject as such:
"lessons_attributes"=>{"0"=>{"lesson_type"=>"Lecture",
"id"=>"348",
"group_ids"=>["37", "38", "39", "40", "41", "42", "131"]}.
To elaborate,
this is the current parameters in full that I am sending:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7r9g2d4lmiSyNZ60i8uos9m1shxVwb0Ly23Tkrshv8w=", "subject"=>{"subject_code"=>"", "subject_name"=>"",
"lessons_attributes"=>{"0"=>{"lesson_type"=>"Lecture", "id"=>"348"},
"1"=>{"lesson_type"=>"Tutorial","id"=>"349"},
"2"=>{"lesson_type"=>"Laboratory","id"=>"350"}},
"remarks"=>""},
"lesson"=>{"group_ids"=>["105", "106", "107"]}, "commit"=>"Update Subject", "id"=>"166"}
The parameters which I want to send however is as such:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7r9g2d4lmiSyNZ60i8uos9m1shxVwb0Ly23Tkrshv8w=", "subject"=>{"subject_code"=>"", "subject_name"=>"",
"lessons_attributes"=>{"0"=>{"lesson_type"=>"Lecture", "id"=>"348","group_ids"=>["37", "38", "39", "40", "41", "42", "131"]},
"1"=>{"lesson_type"=>"Tutorial","id"=>"349",
"group_ids"=>["37", "38", "39", "40", "41", "42", "131"],
"2"=>{"lesson_type"=>"Laboratory","id"=>"350",
"group_ids"=>["37", "38", "39", "40", "41", "42", "131"]}}, "remarks"=>""}, "commit"=>"Update Subject", "id"=>"166"}
I am not sending the parameters right and the problem lies in: `
<%= check_box_tag "lesson[group_ids][]", group.id, f.object.groups.include?(group) , id: "lesson_group_ids_#{group.id}"%>`
And particularly that for my checkbox I am implementing this: lesson[group_ids][].
I have also tried lesson_attributes[group_ids][].
But similarly I could not get the HABTM relationships to save. I am not sure as to what I could do such that the groups saves for each lesson. Any help would be appreciated.
These are the relevant codes:
<%= form_for(@subject) do |f| %>
<div class="field">
<%= f.label :subject_name %><br />
<%= f.text_field :subject_name %>
</div>
<div>
<%= f.fields_for :lessons do |lesson| %>
<%= render "lesson_fields", :f => lesson %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In _lesson_fields.html.erb, I have:
<div class="field">
<%= f.label :lesson_type %><br />
<%= f.text_field :lesson_type,:readonly => true %>
</div>
<div>
<% Group.all.each do |group|%>
<div>
<%= check_box_tag "lesson[group_ids][]", group.id, f.object.groups.include?(group) , id: "lesson_group_ids_#{group.id}"%>
<%= group.group_index %>
</div>
<%end%>
</div>
And for the models
The relevant models:
class Subject < ActiveRecord::Base
attr_accessible :lessons_attributes
has_many :lessons
end
class Lesson < ActiveRecord::Base
belongs_to :subject
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :lessons
end
I beleive you’re missing an index after ‘lesson’ on the name of the checkbox tag (as in ‘lesson[0][group_ids][]’. You can either add that manually, or you may be able to do something with fields_for. I tend to do what you’re doing – loop through all Groups and show them all, decide if they are checked with a boolean test.