Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active admin: Custom check_box input

I need to show a dynamic list of values to show on an Active Admin edit screen as checkboxes, where the list comes from code (not database). I can do that pretty easily, but I can't figure out how to show some of those as being checked. checkboxes

Here's a simplified example of what I'm trying to do:

names = %w(Sam Darcy Ernie)
pairs = Hash[names.zip(names)]
f.input :buddies, as: :check_boxes, collection: pairs, checked: %w(Sam)

What I was hoping for is to show the 3 checkboxes and have the "Sam" box checked. None are checked though. What can I do to control which checkboxes are checked?

like image 943
user605331 Avatar asked Oct 15 '25 19:10

user605331


1 Answers

I ended up with the following that functions as desired:

people = [
  ['Sam', 0, checked: true],
  ['Darcy', 0],
  ['Ernie', 0],
]
f.input :buddies, as: :check_boxes, collection: people

It seems that the array items beyond the first 2 are used to set attributes. So in my case, the "checked" attribute is set, resulting in the element attribute of checked="checked"

I'm still interested in knowing if there is a better way of handling this though.

like image 164
user605331 Avatar answered Oct 18 '25 14:10

user605331



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!