mardi 2 mai 2017

How to save ids and boolean value checkbox_tag Ruby on Rails

I am currently trying to implement an attendance sheet where the user ticks a checkbox next to the user's name; if checked user is present, if unchecked user is absent.

I currently have it working where the attendance sheet lists all users attending the event however, it only saves the attendance id and user id of the checkboxes ticked.

I want the attendance sheet to save all user ids and assign a boolean value to each.

My current view:

 <%= simple_form_for @attendance, :html=> { :multipart=> true } do |f| %>

<div class="checkBoxes">
    <div>
        <%= f.hidden_field :event_id, :value => @event.id %>

    </div>


    <div >
        <% @event.users.each do |user| %>

         <%= check_box_tag "attendance[user_ids][]",user.id,attendance.users.include?(user) %>
            <%= user.name %><br>
    <% end %>
        </div>
    </div>



<div class="actions">
   <%= f.button :submit, 'Save', class: "btn btn-primary" %>
  </div>
<% end %>

The form saves the selected event id and date in an attendances table and then builds the attendance_id and user_id under the attendance_users table.

Database migration for attendance users:

class CreateAttendanceUsers < ActiveRecord::Migration
  def change
    create_table :attendance_users do |t|

      t.integer :attendance_id, :null => false
      t.integer :user_id, :null =>false
      t.boolean :is_absent

      t.timestamps
    end
  end
end

It currently only saves the attendance_id and user_id of the selected checkboxes. How can I:
1.) Save all checked and unchecked users?
2.) Assign each user a boolean value(if checked is_absent is false, if unchecked is_absent is true




Aucun commentaire:

Enregistrer un commentaire