Rails f.check_box设置已选中/未选中的值

Jau*_*uny 14 forms checkbox ruby-on-rails helper

所以我在rails中有一个带有复选框的表单助手; 我希望该复选框在选中或取消选中时将值设置为"thatvalue"或"thisvalue"; 我还没有找到任何地方如何设置它

f.check_box :field
Run Code Online (Sandbox Code Playgroud)

我发现了类似的东西

<%= form.check_box :field, {}, "thisvalue", "thatvalue"  %>
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为我还在我的标签中设置了:class和:style,所以有类似的东西

<%= form.check_box :field, {}, "thisvalue", "thatvalue", :class => "checkbox", :style => "display:none;" %>
Run Code Online (Sandbox Code Playgroud)

错误并告诉我错误的参数数量(4个为5)

所以现在我必须在我的控制器中"破解"它,并根据我的复选框是0还是1来设置我的字段...这非常糟糕.

任何的想法?

Jau*_*uny 37

好吧没关系,我误解了"选项"字段......

答案很简单

<%= f.check_box :field, {:class => "myclass", :style => "mystyle"}, "checked-value", "unchecked-value" %>
Run Code Online (Sandbox Code Playgroud)

它完美地工作:)