Quantico
Quantico

Reputation: 2446

What is the best way to pass many checkboxes values to strong param

I have a page with 10 checkboxes that translate into true/false based on the user selection at the moment my strong param method looks like

def checkbox_params
    params.require(:"{}").permit(:param1, :param2, :param3,...)
end

based on this I iterate over each value and check if it is true/false before I commit the information to my database. If in the future I will add more params this list will become big and I will have a long for iteration. Is there a better practice to pass this information?

Thank you

Upvotes: 0

Views: 93

Answers (1)

AntelopeSalad
AntelopeSalad

Reputation: 1726

You can use an array of check boxes so you only end up with 1 param to permit.

Rails will take each checked value and then append it to an array which ends up being the value of the param. Then you can just .each through the array and do your comparisons there.

Upvotes: 1

Related Questions