'Rails 7 collection_check_boxes not displaying checked items in edit view
Pursuant to this thread, I've tried to get my code to display which items are already checked when you display an edit view. All other fields are pre-populated.
Here's how I have it currently:
%fieldset.border.border-dark.p-2.mb-4
%legend Pronouns
.row.mb-4
= f.collection_check_boxes :pronouns, pronoun_list, :itself, :itself, {include_hidden: false} do |b|
.col-md-4.d-grid.d-block.mb-2
= b.check_box(class: "btn-check", checked: @member.pronouns.split(",").map(&:itself))
= b.label(:"data-value" => b.value, class: "btn btn-outline-dark text-start btn-lg")
.form-group.mb-4
= f.label :pronouns_other, "Other Pronouns"
= f.text_field :pronouns_other, class: "form-control border border-dark"
I need to call include_hidden: false as otherwise the array contains a single blank item as the first in the array.
And here's the helper method it refers to:
def pronoun_list
[
"He/Him/His",
"She/Her/Hers",
"They/Them/Their",
"Zie/Zim/Zir",
"Sie/Sie/Hir",
"Ey/Em/Eir",
"Ve/Ver/Vis",
"Tey/Ter/Tem",
"E/Em/Eir",
"Prefer not to disclose"
]
end
The thing that's different from the other thread is that I'm using a helper method to call the collection of items from while the other thread is calling a collection of objects from the database. So I'm not sure how I'm supposed to get the id's in the first place.
@member.pronouns is a string which contains an Array (I'm using PostgresQL), so first I need to convert it to an array I assume as I read somewhere else, before I can call map on it. but while .map(&:itself) doesn't return any errors, the items that are listed in the pronouns string aren't being checked when the edit view is rendered.
Any ideas?
Solution 1:[1]
After some hours of wrangling with the code, and digging around for other examples, I finally got Rails to work properly.
%fieldset.border.border-dark.p-2.mb-4
%legend Pronouns
.row.mb-4
- pronoun_list.each do |pronoun|
.col-md-4.d-grid.d-block.mb-2
= f.check_box :pronouns, { multiple: true, checked: @member.pronouns.include?(pronoun), class: "btn-check" }, pronoun, false
= f.label :pronouns, pronoun, value: pronoun, class: "btn btn-outline-dark btn-lg text-start"
Explanation:
I have yet to get it to work with a collection_check_boxes; however I did get it to work with a each method and then using multiple: true on each check box.
I also set the checked_value to the pronoun and set the unchecked_value field to false. if I don't set it to false and have multiple selected, I get a bunch of zeros in the array. with false set, I only get the ones I selected in the array.
Solution 2:[2]
I just went through this a bit, the way collection checkboxes works is it will call first and last on object in the collection you pass it. This is so you can have a different label shown to the user and pass a different value to the server. So in your case if you created the collection like
pronoun_list = [ [Him, Him], [Her, Her] ] #contrived example
Then remove the :itself, :itself from the input. The collection checkboxes helper will then call .first
and .last
on each of the nested arrays using Him, Her for both the label for the checkbox and the value for the checkbox sent to the server when submitting the form
When using a collection that comes from the db to begin with like User.all you can tell collection checkboxes what to use for the value and what to use for the label eg :id
and :first_name
where you have :itself, :itself
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Patrick Vellia |
Solution 2 |