jeudi 26 mars 2015

Rails 4 - how to populate checkboxes from a hash?

I have following hash:



CAR_SERVICE = {"Service 1" => 0,
...
"Service 43" => 33}


and I need to take the data from the hash and display them into 34 checkboxes. Then, I have a database table with this structure:



car_services:
- id
- car_id
- service_number # here goes the value (0 to 33) from the hash


Is there a method or a helper that would take this hash and "display it in checkboxes" and after a user save them, then would automatically checked the respective ones (the ones the a user checked)?


Thank you.


PS: There's a select helper that do that (but I need "the same" functionality for checkboxes):



<%= f.select :status, CAR_STATUS, {}




Aucun commentaire:

Enregistrer un commentaire