0

I tried from https://stackoverflow.com/a/4103220/1297435

<% Room.all.in_groups_of(1).each do |room_array| %>
      <% room_array.each do |room| %>
      <%= check_box_tag "student[room_ids][]", room.id, @student.room_ids.include?(room.id), id: dom_id(room) %> <%= label_tag dom_id(room), room.name %><br>
      <% end %>
    <% end %>

when i change in_groups_of(1) to in_groups_of(5), I got the error

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

UPDATE

<% Room.all.in_groups_of(5).each do |room_array| %>
          <% room_array.compact.each do |room| %>
          <%= check_box_tag "student[room_ids][]", room.id, @student.room_ids.include?(room.id), id: dom_id(room) %> <%= label_tag dom_id(room), room.name %><br>
          <% end %>
        <% end %>

Why not display to column?

enter image description here

1 Answer 1

1

in_groups_of method filled array with nil object when count of objects is less in array. so you need to remove nil before rendering them.

use compact to remove nil objects

<% Room.all.in_groups_of(1).each do |room_array| %>
      <% room_array.compact.each do |room| %>
      <%= check_box_tag "student[room_ids][]", room.id, @student.room_ids.include?(room.id), id: dom_id(room) %> <%= label_tag dom_id(room), room.name %><br>
      <% end %>
    <% end %>
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, Thank's, but not display to column, can you help me why be like this i.cubeupload.com/vXGixi.png

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.