Rails f.check_box set checked/unchecked values

Jauny picture Jauny · May 15, 2013 · Viewed 34.1k times · Source

so I got a form helper in rails with a checkbox; I want that checkbox to have values as "thatvalue" or "thisvalue" when checked or unchecked; I haven't found anywhere how to set this up with

f.check_box :field

I found something like that

<%= form.check_box :field, {}, "thisvalue", "thatvalue"  %>

but it doesn't work, because I also set :class and :style inside my tag, so having something like

<%= form.check_box :field, {}, "thisvalue", "thatvalue", :class => "checkbox", :style => "display:none;" %>

errors and tells me wrong number of arguments (5 for 4)

so right now I have to "hack" it in my controller, and set my field depending on if my checkbox is 0 or 1... which is pretty bad.

any idea?

Answer

Jauny picture Jauny · May 15, 2013

ok nevermind, I misunderstood the "options" field...

the answer is simply

<%= f.check_box :field, {:class => "myclass", :style => "mystyle"}, "checked-value", "unchecked-value" %>

and it works perfectly :)