Skip to content
Eric Draut edited this page Sep 16, 2017 · 6 revisions

Example

<%= form_tag bulk_assign_manage_workorders_path, id: "workorder_bulk_edit_form", style: 'display: none;' do |f| %>
  <%= select_tag :assignee_id, options_from_collection_for_select(@operations_users, :id, :short_name) %>
  <button type="submit">Bulk Assign</button>
  <a <%= fake_deselect_attrs %>>deselect all</a>
<% end &>

<% @workorders.each do |workorder| %>
  <div <%= fake_checkbox_attrs('#workorder_bulk_edit_form', 'workorder_ids[]', workorder.id, toggle_form: true) %> >
  </div>
  <%= workorder.description %>
<% end %>

In the above example, a list of workorders shows checkboxes next to each. Clicking one of the checkboxes will cause the bulk assignment form above to appear. Deselecting all of the workorders causes the form to disappear again. Submitting the form will send not only the assignee_id, but an array of the selected workorder ids for assignment.

The Helpers

fake_checkbox_attrs(form_selector, field_name, field_value, toggle_form: false)

Use this helper to designate a checkbox that should be connected to the form described by form_selector.

field_name - the name of the input element that will be added to the form if this box is checked.

field_value - the value that will be set on the input element.

toggle_form - tells hooch to hide the form if all the checkboxes are deselected at the same time.

fake_deselect_attrs

Use this helper to tell hooch that clicking this element should deselect all the checkboxes associated with the enclosing form element.