Yogzzz
Yogzzz

Reputation: 2795

Rails check box resetting to default

I have a few check boxes in my view set to default as active:

<%= check_box "product[pr_attributes]", "ag_type", {:checked => @product.issenior?, :multiple => true, :checked => true}, checked_value = "ag:senior", unchecked_value = nil %>Senior(65-100)

The problem is, when I uncheck one of the defaults and save the changes, it defaults back to the checked state. How can I solve this?

Upvotes: 0

Views: 160

Answers (2)

Fred
Fred

Reputation: 286

i think the best way to do this in your case is use check_box_tag since your doing multiple answers for one attribute

syntax

check_box_tag "id", "value", "boolean_if_checked"

so in your case:

<%= check_box_tag "product[pr_attributes][]", "ag_type", @product.issenior?, { } %>

Then just add the other attributes on the hash after the @product.issenior?

This way, you can create multiple checkboxes for pr_attributes, and then when you submit the form, pr_attributes will be an array of your choices.

Upvotes: 0

natedavisolds
natedavisolds

Reputation: 4295

Did you mean to have two option keys for :checked?

Mostly like the second one :checked => true is causing your problem.

Upvotes: 2

Related Questions