簡體   English   中英

Rails:帶&&和||的條件語句

[英]Rails: Conditional statement with && and ||

我目前有這個:

<% if h.ab_template.AB_filterParent == 0 && h.status != 'geo_enabled' %>

我想說的是,如果h.ab_template.AB_filterParent == 0 || 零。 我該怎么做?

我試過了,但是沒有用:

    <% if (h.ab_template.AB_filterParent == 0 || nil) && h.status != 'geo_enabled' %>

很想知道我輸錯了什么或實現不正確!

干杯

如果您確定h.ab_template.AB_filterParent始終是數字(可以用引號引起來),則可以嘗試以下操作

<% if h.ab_template.AB_filterParent.to_i == 0 && h.status != 'geo_enabled' %>

否則,如果h.ab_template.AB_filterParent有可能像“ abc”“ 0asd”等,請嘗試:

<% if (h.ab_template.AB_filterParent.nil? || h.ab_template.AB_filterParent == 0) && h.status != 'geo_enabled' %>

通常,nil和0不應表示同一件事。 嘗試通過在遷移表中分配默認值零來消除AB_filterParent為nil的可能性,然后再單擊此代碼。 我不知道您的模型如何,所以我什至無法顯示一個示例。 使用to_i == 0的主要問題是,僅當AB_filterParent為整數或nil時,它才有效。

0.5.to_i == 0
"asdasd".to_i == 0

所以這很奇怪。

另一種方法是在操作或其他模型方法中甚至在after_create回調中對其進行初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM