Predefined tag checkboxes with ActsAsTaggableOn
Since these predefined values were quite like tags, I chose ActsAsTaggableOn to provide the model- and controller-level functionality. But the ActsAsTaggableOn documentation prefers to skirt the matter of user interface, with but one paltry example.
In my case, I wanted to:
- predefine the list of available values
- show a checkbox for each available value
- store the selected choices as ActsAsTaggableOn dictates
- not hard-code the values in my view
A colleague recently used a form helper that stuck with me—
collection_check_boxes, which renders a set of check boxes when provided a collection of options—though its documentation, too, left something to be desired, especially since I had a simple array of values and not the collection of models used in the published examples.
So, with some experimentation, here’s how I wired it all together: