Ruby-on-rails 检查 Rails 快捷方式中是否不为零且不为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22970057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if not nil and not empty in Rails shortcut?
提问by user3048402
I have a show page for my Users and each attribute should only be visible on that page, if it is not nil and not an empty string. Below I have my controller and it is quite annoying having to write the same line of code @user.city != nil && @user.city != ""for every variable. I am not too familiar with creating my own methods, but can I somehow create a shortcut to do something like this: @city = check_attr(@user.city)? Or is there a better way to shorten this procedure?
我有一个供我的用户使用的显示页面,并且每个属性应该只在该页面上可见,如果它不是 nil 并且不是空字符串。下面我有我的控制器,必须@user.city != nil && @user.city != ""为每个变量编写相同的代码行非常烦人。我对创建自己的方法不太熟悉,但是我可以以某种方式创建一个快捷方式来做这样的事情:@city = check_attr(@user.city)?或者有没有更好的方法来缩短这个过程?
users_controller.rb
用户控制器.rb
def show
@city = @user.city != nil && @user.city != ""
@state = @user.state != nil && @user.state != ""
@bio = @user.bio != nil && @user.bio != ""
@contact = @user.contact != nil && @user.contact != ""
@twitter = @user.twitter != nil && @user.twitter != ""
@mail = @user.mail != nil && @user.mail != ""
end
回答by tadman
There's a method that does this for you:
有一种方法可以为您执行此操作:
def show
@city = @user.city.present?
end
The present?method tests for not-nilplus has content. Empty strings, strings consisting of spaces or tabs, are considered not present.
present?not- nilplus的方法测试有内容。空字符串、由空格或制表符组成的字符串被认为不存在。
Since this pattern is so common there's even a shortcut in ActiveRecord:
由于这种模式非常普遍,ActiveRecord 中甚至还有一个快捷方式:
def show
@city = @user.city?
end
This is roughly equivalent.
这大致相当。
As a note, testing vs nilis almost always redundant. There are only two logically false values in Ruby: niland false. Unless it's possible for a variable to be literal false, this would be sufficient:
需要注意的是,测试 vsnil几乎总是多余的。Ruby 中只有两个逻辑错误值:nil和false。除非变量可能是literal false,否则就足够了:
if (variable)
# ...
end
This is preferable to the usual if (!variable.nil?)or if (variable != nil)stuff that shows up occasionally. Ruby tends to wards a more reductionist type of expression.
这比平常if (!variable.nil?)或if (variable != nil)偶尔出现的东西更可取。Ruby 倾向于使用更简化的表达方式。
One reason you'd want to compare vs. nilis if you have a tri-state variable that can be true, falseor niland you need to distinguish between the last two states.
您想要比较 vs. 的一个原因nil是,如果您有一个三态变量可以是true,false或者nil您需要区分最后两个状态。
回答by Thank you
You can use .present?which comes included with ActiveSupport.
你可以使用.present?它包含在 ActiveSupport 中。
@city = @user.city.present?
# etc ...
You could even write it like this
你甚至可以这样写
def show
%w(city state bio contact twitter mail).each do |attr|
instance_variable_set "@#{attr}", @user[attr].present?
end
end
It's worth noting that if you want to test if something is blank, you can use .blank?(this is the opposite of .present?)
值得注意的是,如果你想测试某个东西是否为空白,你可以使用.blank?(这是 的反义词.present?)
Also, don't use foo == nil. Use foo.nil?instead.
另外,不要使用foo == nil. 使用foo.nil?来代替。

