Forum: Ruby on Rails HABTM Checkboxes

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bfbdca2c320512fa7f9613738ed0ca39?d=identicon&s=25 unknown (Guest)
on 2013-07-28 19:42
(Received via mailing list)
I have a Course model which habtm categories. I am trying to have
checkboxes to add or delete categories from the new and update courses


# == Schema Information
# Table name: courses
#  id                     :integer          not null, primary key
#  course_number          :integer
#  partner                :string(255)
#  title                  :string(255)
#  length                 :integer
#  includes_certification :boolean
#  overview               :text
#  price                  :integer
#  created_at             :datetime
#  updated_at             :datetime
#  more_link              :string(255)

class Course < ActiveRecord::Base
  self.per_page = 8
  has_and_belongs_to_many :course_dates
  has_and_belongs_to_many :categories
  attr_accessible :course_number, :partner, :title, :length,
:includes_certification, :overview, :more_link, :price, :tags,
:course_dates_attributes, :categories_attributes
  accepts_nested_attributes_for :course_dates, allow_destroy: true
  accepts_nested_attributes_for :categories, allow_destroy: true

# == Schema Information
# Table name: categories
#  id            :integer          not null, primary key
#  name          :string(255)
#  created_at    :datetime
#  updated_at    :datetime

class Category < ActiveRecord::Base
  has_and_belongs_to_many :courses
  attr_accessible :name

My form is being built from this partial:

= simple_form_for(@course) do |f|
  = f.error_notification

    = f.input :course_number
    = f.input :partner
    = f.input :title
    = f.input :length
    = f.fields_for :course_dates do |builder|
      = render 'course_date_fields', f: builder
    = link_to_add_fields "Add Date", f, :course_dates
    = f.input :includes_certification
    = f.input :overview
    = f.input :more_link
    = hidden_field_tag "course[category_ids][]", nil
    - Category.all.each do |category|
      = check_box_tag "course[category_ids][]",,
@course.category_ids.include?(, id: dom_id(category)
      = label_tag dom_id(category),
    = f.input :price

    = f.button :submit

I tried using simple_form's f.association as: :check_boxes and also the
f.collection_check_boxes but both end in the same problem. I can see the
checkboxes but when I click update it doesn't save the categories I
or unchecked.

I have been trying to figure out why, figured it was a problem with mass
assignment but I don't know what else I can do with attr_accessible to
it work.
A47e0a6beeb9d048ff054fc1c3a97418?d=identicon&s=25 Walter Davis (walterdavis)
on 2013-07-28 21:20
(Received via mailing list)
Have a search on Railscasts. This is covered there.

This topic is locked and can not be replied to.