Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:33:04+00:00 2026-06-17T19:33:04+00:00

I am trying to implement a checkbox in a nested form. I have 3

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:33:05+00:00Added an answer on June 17, 2026 at 7:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement image as checkbox. I have a list with 2
I have been trying to implement a CheckBox Node Tree, where the parent nodes
I am trying to implement a gridview with checkbox column on asp.net (VB). when
Iam trying to implement contacts with a checkbox and selecting multiple contacts and saving
I am currently trying to implement a ViewPager . I have two pages within
I am trying to implement the following code: public CheckBox checkboxer() { final CheckBox
I'm trying to make a dynamic PDF form using Adobe LiveCycle designer and have
I'm trying to implement something like the following graphic below; where the user has
I am trying to implement user preferences. A problem I have is that if
I have am trying to implement something along the lines of Slickgrid, column with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.