Rails 3 - 如何在视图中创建三元条件?
我有三元运算符,我正在尝试将此三元运算符放入复选框中,但我仍然在编写时出错(语法错误)...
所以我想询问帮助,该怎么做...
CAR: <%= f.check_box :car, :value => 2, ((f.sex == 2) ? (:checked => true) : (:checked => false)) %>
I have ternary operator and I am trying this ternary operator put into checkbox, but I am still making fault in writing (syntax error)...
So I would like to ask about help, how to do...
CAR: <%= f.check_box :car, :value => 2, ((f.sex == 2) ? (:checked => true) : (:checked => false)) %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里不需要三元运算符。试试这个:
CAR: <%= f.check_box :car, :value => 2、:检查=> (f.sex == 2) %>
另外,您的问题来自这样一个事实:在
Hash
文字中,您无法有条件地定义键,因此:{:a => (:b || :c)}
有效{:b ? (a: => :b) : (:a => :c)}
无效You don't need a ternary operator here. Try this instead:
CAR: <%= f.check_box :car, :value => 2, :checked => (f.sex == 2) %>
Also your problem comes from the fact that in a
Hash
literal you can't define keys conditionally, so:{:a => (:b || :c)}
is valid{:b ? (a: => :b) : (:a => :c)}
is invalid<%= f.check_box :car, :value => 2、:检查=> f.性别 == 2 ? true : false %>
可以使用,但可以缩短为<%= f.check_box :car, :value => 2、:检查=> f.sex == 2 %>
!<%= f.check_box :car, :value => 2, :checked => f.sex == 2 ? true : false %>
will work, but can be shortened to<%= f.check_box :car, :value => 2, :checked => f.sex == 2 %>
!