Rails f.check_box设置选中/未选中的值 [英] Rails f.check_box set checked/unchecked values

查看:783
本文介绍了Rails f.check_box设置选中/未选中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在rails中有一个复选框的窗体助手;
当选中或取消选中时,我想要该复选框的值为thatvalue或thisvalue;
我没有找到任何地方如何设置这个与

  f.check_box:field 

我发现了这样的东西。

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

但它不工作,因为我还在标签中设置了class和:style具有类似

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

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



所以现在我必须在我的控制器黑客,并设置我的字段取决于我的复选框是0还是1 ...这是很糟糕。



任何想法?

解决方案

ok nevermind,我误解了options p>

答案只是

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

它完美地工作:)


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?

解决方案

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

这篇关于Rails f.check_box设置选中/未选中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆