在一个葡萄实体中,我想只显示一个字段(不是零?)没有运气。
我正在尝试这段代码,但根本无法正常工作,但始终隐藏该字段。
expose :winner, :using => PlayerEntity, :unless => { :winner => nil }
我认为代码本身解释了我真正需要的东西,但正如我所说,我没有得到预期的结果。
任何线索?
好的,检查葡萄实体的代码我发现你需要将这个块作为Ruby Proc传递。此代码将按预期工作:
expose :winner, :using => PlayerEntity, :unless => Proc.new {|g| g.winner.nil?}
希望它可以帮助某人。干杯
https://github.com/ruby-grape/grape-entity#expose-nil
Grape Entity现在提供expose_nil
选项,所以这应该可以解决问题:
expose :winner, :using => PlayerEntity, expose_nil: false