module BulmaFormRails::SubformHelpers

kevinweller.name

This Ruby on Rails Helper module supports tabular editable multi-object subforms.

Public Instance Methods

bulma_child_objects(collection, name, attributes_key, add_child_path, options = {}) click to toggle source

Renders a child objects collection management UI subform, including row addition and deletion buttons by default.

  • collection - an array of serializable objects

  • name - the lower-case singular name of the object for display purposes

  • attributes_key - a symbolic key into the attribute reference hash retrievable via the lookup_attributes(key) method defined on the controller as a helper_method, eg :object for:

    {object: ["attribute_1", "attribute_2"]}
    
  • add_child_path - the controller URL path for the action to add a child object to the collection

  • options - a hash of additional options to pass to the partials

    • system_controlled - true if you want no row add or delete buttons, false or unspecified if you do want add and delete buttons

    • total_columns - an array of integers representing column numbers requiring arithmetic totals at the bottom of the subform, eg:

      [3, 4]
      
    • container - a string representing an array index expression for a row if not a single index, eg:

      "[related_reservations][#{related_reservation_counter}]"
      

To use this helper method, you will first need to do the following:

  1. Call bulma_child_forms from the specific controller (but outside any existing method definitions) for your view, passing in a hash of the object collections to be managed along with their attributes, for example, {object1: %w[attr1 attr2], object2: %w[attr3 attr4]}.

  2. Add a route to the add_child action for the same controller, for example, put 'users/add_child', to: 'users#add_child'.

# File lib/bulma_form_rails/subform_helpers.rb, line 27
def bulma_child_objects(collection, name, attributes_key, add_child_path, options = {})
  render partial: 'bulma_form_rails/children', object: collection, locals: {url: add_child_path, name: name, attributes_key: attributes_key}.merge(options)
end