如何提交在Rails布尔参数? [英] How do I submit a boolean parameter in Rails?
问题描述
我提交的参数 SHOW_ALL
与值真正
。此值不与模型相关联
I'm submitting a parameter show_all
with the value true
. This value isn't associated with a model.
我的控制器分配此参数实例变量:
My controller is assigning this parameter to an instance variable:
@show_all = params[:show_all]
然而, @ show_all.is_a?字符串
和如果@show_all ==真
总是失败。
确实Rails的解析什么样的价值观作为布尔?我怎么能明确指定我的参数是一个布尔值,而不是一个字符串?
What values does Rails parse as booleans? How can I explicitly specify that my parameter is a boolean, and not a string?
推荐答案
我想对探究性的回答发表评论,但我不能这样做,但我会后这是一个答案。
I wanted to comment on zetetic answer but as I can't do that yet I'll post this as an answer.
如果您使用
@show_all =参数[:SHOW_ALL] ==1
,那么你可以删除?真:假
,因为 PARAMS [:SHOW_ALL] ==1
语句本身将评估为真或假,因此不需要三元操作符。
then you can drop ? true : false
because params[:show_all] == "1"
statement itself will evaluate to true or false and thus ternary operator is not needed.
这篇关于如何提交在Rails布尔参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!