Rails f.check_box set checked/unchecked values

37,501

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 :)

Share:
37,501
Jauny
Author by

Jauny

Updated on May 16, 2020

Comments

  • Jauny
    Jauny almost 4 years

    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?