Rails:如何检查列是否有值?
-
08-07-2019 - |
题
我该如何做到这一点?
<% for agent in @broker.agents %>
...
<% if agent.cell %><span class="cell-number">Cell: <%= agent.cell %></span><% end %>
...
<% end %>
我想测试以查看代理是否有单元格编号,如果是,则显示条件内的内容。我目前所做的似乎不起作用;它只显示<!>“; Cell:<!>”;
思想?
解决方案
这就是你要求的:
<% for agent in @broker.agents %>
<% unless agent.cell.blank? %>
<span class="cell-number">Cell: <%= agent.cell %></span>
<% end %>
<% end %>
细胞?无论单元格是零还是空字符串,该方法都有效。 Rails为所有ActiveRecord属性添加了类似的功能。这看起来会更好一些:
<% for agent in @broker.agents %>
<span class="cell-number">
Cell: <%= agent.cell? ? "none given" : agent.cell %>
</span>
<% end %>
问号和冒号快速形成<!>;如果?然后:else <!> quot;声明。上面的代码中有两个问号,因为一个是方法名称单元格的一部分?另一个是if / then / else结构的一部分。
其他提示
我正在对此问题给出非常详细的解答<!>“如何检查列是否有值? <!>”。
首先,重要的是要注意一个属性可以包含四种值。
- nil 值,即<!>“; nil <!>存储在数据库中
- 空值,即<!>“<!>”;带有无空格 的空字符串
- 空字符串包含空格 <!> quot; QUOT <!>;
- 数据库中存在的值,即非空字符串。 醇>
-
nil value = <!> gt;引发异常
2.2.2 :037 > object.attribute => nil 2.2.2 :025 > object.attribute.empty? NoMethodError: undefined method `empty?' for nil:NilClass
-
empty 值,即<!> quot; <!> quot;带有无空格
的空字符串2.2.2 :037 > object.attribute => "" 2.2.2 :025 > object.attribute.empty? true
-
空字符串包含空格 <!> quot; QUOT <!>;
2.2.2 :041 > object.attribute => " " 2.2.2 :042 > object.attribute.empty? => false
-
数据库中存在的值,即非空字符串。
2.2.2 :045 > object.attribute => "some value" 2.2.2 :046 > object.attribute.empty? => false
醇>
-
nil 值,即<!>“; nil <!>存储在数据库中
2.2.2 :049 > object.attribute => nil 2.2.2 :050 > object.attribute.nil? => true
-
空值,即<!>“<!>”;带有无空格
的空字符串2.2.2 :053 > object.attribute => "" 2.2.2 :054 > object.attribute.nil? => false
-
空字符串包含空格 <!> quot; QUOT <!>;
2.2.2 :057 > object.attribute => " " 2.2.2 :058 > object.attribute.nil? => false
-
数据库中存在的值,即非空字符串。
2.2.2 :061 > object.attribute => "some value" 2.2.2 :062 > object.attribute.nil? => false
醇>
-
nil 值,即<!>“; nil <!>存储在数据库中
2.2.2 :065 > object.attribute => nil 2.2.2 :066 > object.attribute.blank? => true
-
空值,即<!>“<!>”;带有无空格
的空字符串2.2.2 :069 > object.attribute => "" 2.2.2 :070 > object.attribute.blank? => true
-
空字符串包含空格 <!> quot; QUOT <!>;
2.2.2 :073 > object.attribute => " " 2.2.2 :074 > object.attribute.blank? => true
-
数据库中存在的值,即非空字符串。
2.2.2 :075 > object.attribute => "some value" 2.2.2 :076 > object.attribute.blank? => false
醇>
-
nil 值,即<!>“; nil <!>存储在数据库中
2.2.2 :088 > object.attribute => nil 2.2.2 :089 > object.attribute.present? => false
-
空值,即<!>“<!>”;带有无空格
的空字符串2.2.2 :092 > object.attribute => "" 2.2.2 :093 > object.attribute.present? => false
-
空字符串包含空格 <!> quot; QUOT <!>;
2.2.2 :096 > object.attribute => " " 2.2.2 :097 > object.attribute.present? => false
-
数据库中存在的值,即非空字符串。
2.2.2 :100 > object.attribute => "some value" 2.2.2 :101 > object.attribute.present? => true
醇>
以下是在这种情况下可以使用的所有现有方法(Ruby 2.2.2)的详细行为。
第一种方法: .empty?
第二种方法: .nil?
第三种方法: .blank?
第四种方法: .present?
根据您所面临的情况,您可以使用四种中的任何一种。
由于
if !agent.cell.blank?
有效。
agent.cell?似乎与agent.cell.blank一样工作?在RoR。
<% @broker.agents.each do |agent| %>
...
<% unless agent.cell.empty? %>
<span class="cell-number">Cell: <%= agent.cell %></span>
<% end %>
...
<% end %>
我发现#each,unless
和cell.empty?
的使用乍一看更容易理解。