Bargain23
Bargain23

Reputation: 1973

Rails not saving checkbox input from model with many-to-many association

I have two models Teacher and Student, and I established a HABTM relationship between two of them. I have a form for each of them to create a new record in the database. For example, in the create teacher form, the user can enter the teacher's name as input, and tick checkboxes containing the names of different students. If the user ticks a box, the input will not be saved, but if the checkbox is left blank and the user simply inputs the name, a new record is saved. Weirdly enough, if the user updates the teacher, that's only time the user can choose students to include in the teacher's record.

This is how some of the methods in TeacherController looks like:

def new
  @teacher = current_user.teachers.build
end

def create
  @teacher = current_user.teachers.build(teacher_params)

  if @teacher.save
    redirect_to root_path
  else
    render 'new'
  end
end

def edit
end

def update
  if @teacher.update(teacher_params)
    redirect_to teacher_path(@teacher)
  else
    render 'edit'
  end
end

private
  def teacher_params
    params.require(:teacher).permit(:name, :student_ids => [])
  end

  def search_teacher
    @teacher = Teacher.find(params[:id])
  end

Below is the form I use to create a new teacher and update an existing teacher:

<%= simple_form_for @teacher do |f| %>
  <%= f.input :name, label: "Teacher's Name" %>
  <label>Students</label>
  <br>
  <%= f.collection_check_boxes :student_ids, Student.all, :id, :name %>
  <br>
  <%= f.button :submit %>
<% end %>

It might also help noting that I only encountered this problem after introducing user authentication with devise in my application.

Upvotes: 0

Views: 327

Answers (1)

fool-dev
fool-dev

Reputation: 7777

Follow steps like this

in the controller

def create
   @teacher = Teacher.new(teacher_params)
   @teacher.user= current_user
   if @teacher.save
     # code
   else
     # code
   end
end

private
def teacher_params
   params.require(:teacher).permit(:name, student_ids: [])
end

The form

<%= f.collection_check_boxes :student_ids, Student.all, :id, :name do |cb| %>
    <% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text } %>
<% end %>

See the collection_check_boxes and this relationship.

If don't work it would be better to post Models with relationships.

Upvotes: 2

Related Questions