Search code examples
ruby-on-railsrubyhamlsimple-form

Making multiselect work with simpleform, in rails


My aim is to have a teacher be able to select multiple curriculums they are able to teach, I am using simple-forms. It could be checkbox or tags

Below is my setup.

TeacherProfile model

class TeacherProfile < ApplicationRecord
  belongs_to :subject 
  belongs_to :grade
  # belongs_to :curriculum
  has_many :curriculums
  belongs_to :user

  validates :national_identity, :curriculum_ids, :grade_id, :subject_id,
    :employer, :years_teaching, 
    :about_me, presence: true

  def self.teachers_classes(user_id)
    ClassRoom.where(user_id: user_id)
  end
end

Curriculum model

class Curriculum < ApplicationRecord
  has_many :student_profiles
  has_many :teacher_profiles
end

Form

.py-5.bg-light-v2
  .container
    .row.align-items-center
        .col-md-6
            %h2 Teacher Profile

%section.padding-y-50.border-bottom.border-light
  .container
    .row
      .col.col-md-8.mb-5
        = simple_form_for(@teacher_profile) do |f|
          = f.error_notification
          = f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?

          .border-bottom.mb-4.pb-4
            %h5.mb-4 
              Personal Details
              %br
              %small.text-muted select what applies to you, tick if it applies to you.

            = f.input :national_identity, label: "Enter your National ID or Passort", placeholder:"216331771"
            = f.input :special_education, label: "I have Special education skills"
            = f.input :has_hardware, label: "I have a computer & Internet"
            = f.input :is_available, label: "I am available to tutor"
            = f.input :has_tsc_certificate, label: "I have a TSC certificate"
            = f.input :has_good_conduct, label: "I have  good conduct certificate"

          .border-bottom.mb-4.pb-4
            %h5.mb-4 Professional Details
            = f.input :years_teaching, label: "I have been teaching since"
            .row
              -# .col= f.association :curriculum, prompt: "Select the curriculum you use"
              .col= f.association :curriculums, input_html: {:multiple => true} 
              .col= f.association :grade, prompt: "Select the grades you teach"
            = f.association :subject, prompt: "Select the subjects you teach"
            = f.input :employer, label: "I currently work at", placeholder:"Enter name of employer if applicable or leave blank"

          .border-bottom.mb-4.pb-4
            %h5.mb-4 About your passion
            = f.input :about_me, as: :text, label: "Tell us why you love teaching", placeholder:"I am passionate about teaching..."

          .form-actions
            = f.button :submit, class: "btn btn-primary btn-lg mt-2"

I am getting an error:

Curriculum ids can't be blank

What could I be missing?


Solution

  • Build a joining table with a many-to-many relationship. This separates the problem, allows adding the extra fields without too much work.

    Create migration table/model X, and reference the desired fields, in this case use subjects, curriculum, grades and teacher_profiles.

    Create a many-to-many relationship in this table, then make the new model nested under teacher_profiles.

    The video "Rails - Nested Forms in Rails | [Tutorial]" is useful.