Setting specific check_boxes as pre-checked #878

Closed
alexeiemam opened this Issue Jul 30, 2012 · 3 comments

Comments

Projects
None yet
2 participants

How is it possible to pre-check a specific checkbox?

Using formtastic 2.1.1

The following fails to apply the checked status to the second box

f.input :options_galore, 
    :as => :check_boxes, 
    :collection => [
      ["option a", 100 ,{:checked => false}],
      ["option b", 200 ,{:checked=> true}],
      ["option c", 300 ,{:checked => false}]
    ]

The below (select, radio, and select multiple, respectively) all succeed.

f.input :options_galore, 
    :as => :select, 
    :selected => 200,
    :collection => [
      ["option a", 100],
      ["option b", 200],
      ["option c", 300]
    ]

f.input :options_galore, 
    :as => :select, 
    :collection => [
      ["option a", 100 ,{:selected=> false}],
      ["option b", 200 ,{:selected=> true}],
      ["option c", 300 ,{:selected=> false}]
    ]
f.input :options_galore, 
    :as => :radio, 
    :collection => [
      ["option a", 100 ,{:checked=> false}],
      ["option b", 200 ,{:checked=> true}],
      ["option c", 300 ,{:checked=> false}]
    ]
f.input :options_galore, 
    :as => :select, 
    :collection => [
      ["option a", 100 ,{:selected=> false}],
      ["option b", 200 ,{:selected=> true}],
      ["option c", 300 ,{:selected => true}]
    ],
    :input_html => {:multiple => true}

the following succeeds at checking all the checkboxes

f.input :options_galore, 
    :as => :check_boxes, 
    :collection => [
      ["option a", 100 ],
      ["option b", 200 ],
      ["option c", 300 ]
    ],
    :input_html => {:checked => true}
Owner

justinfrench commented Jul 31, 2012

I'm really surprised that ["option a", 100 ,{:selected=> false}] works on selects and radios at all! If it does work, I'd love someone to check for test coverage and documentation, and then it should be pretty easy to add the same functionality to checkboxes. Pull requests accepted, but I don't have time to do this at the moment.

The other (recommended) way to check/select some radio/checkbox/select values is to set the on the model instance (either as part of instantiation in the model, or in the controller).

Thanks for the response. Could you point me to any documentation that pertains to the recommended method?

Owner

justinfrench commented Aug 1, 2012

@alexeiemam I've added basic examples to the wiki page that deals with the deprecation of the old :selected option:
https://github.com/justinfrench/formtastic/wiki/Deprecation-of-:selected-option

The specifics of how you deal with your model are up to you of course!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment