Rails - HABTM find or create nested form

187 views Asked by At

I found this.

My question is now how do I create the form, I am trying:

        #students.row
          = f.fields_for :classrooms_students, f.object.classrooms_students do |cs|
            = cs.fields_for :student, cs.object.student do |s|
              / .record is used to fill fields when teacher clicks a .result
              .record
                .col-sm-6 style=("margin-top: 10px;")
                  = s.label :name
                  = s.text_field :name, class: 'form-control autocomplete', data: {autocomplete: :name}
                  .results
                .col-sm-6 style=("margin-top: 10px;")
                  = s.label :email
                  = s.text_field :email, class: 'form-control autocomplete', data: {autocomplete: :email}
                  .results
            .col-sm-12 style=("margin-top: 10px;")
              = cs.link_to_remove 'Remove Student', class: 'btn btn-danger'
          .col-sm-12 style=("margin-top: 10px;")
            = f.link_to_add 'Add Student', :classrooms_students, "data-target" => '#students', class: 'btn btn-primary'

The problem is that in server I am getting:

{"classroom"=>
  {"name"=>"qwerqqeq",
   "description"=>"",
   "note"=>"",
   "classrooms_students_attributes"=>
    {"0"=>
      {"student_attributes"=>{"name"=>"vsrjh", "email"=>"[email protected]", "id"=>"33"},
       "_destroy"=>"false",
       "id"=>""},
     "1"=>
      {"student_attributes"=>{"name"=>"cvbcbc", "email"=>"[email protected]", "id"=>"32"},
       "_destroy"=>"false",
       "id"=>""}}}}

Where id on the join hash is '' how would Rails look for the record?

So I am getting:

undefined method `to_sym' for nil:NilClass.

Should I add an id column to join table?

1

There are 1 answers

0
sites On BEST ANSWER

I added an index:

    add_column :classrooms_students, :id, :primary_key