Ruby-on-rails Rails f.check_box 设置选中/未选中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16573508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:00:13  来源:igfitidea点击:

Rails f.check_box set checked/unchecked values

ruby-on-railsformscheckboxhelper

提问by Jauny

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

所以我在 rails 中得到了一个带有复选框的表单助手;我希望该复选框在选中或未选中时具有“那个值”或“这个值”的值;我还没有找到如何设置它

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

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

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

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

错误并告诉我错误的参数数量(5 对 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.

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

any idea?

任何的想法?

回答by Jauny

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

它完美地工作:)