2

i have checkbox in simple form:

= f.input :status, as: :boolean

but i don't want values to be 0 or 1. i want them to be Strings, like eg 'not_done' and 'completed'. how can i achieve that? i ran into some kind of solution on github thread, but i don't really know what is it doing, plus, it always give me "new" as value, doesnt matter if the field is checked or not...

= f.input :status, as: :boolean do
    - f.check_box :status, {}, "new", "confirmed"
6
  • 1
    The "check_box" method should work: api.rubyonrails.org/classes/ActionView/Helpers/…. You don't need the first "f.input". Just the checkbox. Commented Aug 20, 2013 at 8:56
  • but how it is supposed to look like, doc isnt it really making it clear for me with the arguments...:/ Commented Aug 20, 2013 at 9:15
  • Kuba, it should look exactly like it already does: f.check_box :Status, {}, "new", "confirmed". But just that part. Completely get rid of the f.input :status. That is shadowing the checkbox result. Commented Aug 20, 2013 at 9:28
  • tried it, and it is giving me value: "new" whether it is checked or not Commented Aug 20, 2013 at 9:30
  • you can use option box instead Commented Aug 20, 2013 at 9:41

2 Answers 2

4

You can do

<%= f.input :status, as: :boolean,
                     checked_value: 'completed',
                     unchecked_value: 'not_done'
                     %>

This will set the values as completed when checked and not_done when unchecked.

However, if you want to go ahead and display the same, you can do:

<%= f.input :status, as: :boolean,
                     checked_value: 'completed',
                     unchecked_value: 'not_done',
                     input_html: {
                       name: 'status_checkbox', # not a must, will help for identification and testing
                       checked: params[:status] == 'completed' ? 'Completed' : 'Not Done'
                     } %>
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to go with check_box then its possible:

<%= f.input :status, :as => :check_boxes, collection: [['Completed', 'completed']]  %>

But you should not expect two values from a check_box. It will carry only one value. the reverse will be considered if the check box is not checked.

7 Comments

this solution doesnt give me no value at all in check box...:/
here is the out put html: <ol class="choices-group"> <li class="choice"> <label for="object_status_completed"> <input id="object_status_completed" type="checkbox" value="completed" name="object[status][]"> Completed </label> </li> </ol>
and what am i supposed do with it?:)
ok, i was wrong, it does give me value, but it is always the same value
you can use option box if you need multiple values
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.