新手问题 Rails 和 Grape 返回的 JSON 中存在<null>

jianxd · 2015年06月10日 · 最后由 hammer 回复于 2015年06月12日 · 2954 次阅读

rails 和 grape 提供移动 APP 的 API,使用 entity 返回的结果中存在,如果我想在服务端返回的时候把这些替换成空字符串,该怎么实现啊?

返回的时候做个处理。或者你把可能为空的字符串添加一个默认的空值。

expose :doctor_name do |model, opts|
    model.doctor.try(:name) || ""
end
module API
  class BaseEntity < Grape::Entity
    format_with(:null) { |v| v.blank? ? "" : v } //here
    format_with(:iso_timestamp) { |dt| dt.iso8601 if dt }
  end
end

module API
  class KldTwoB < BaseEntity
    expose :ceo, format_with: :null //this
    expose :cto, format_with: :null
    expose :deadline, format_with: :iso_timestamp
  end
end

办法原来这么多

5 楼 已删除
需要 登录 后方可回复, 如果你还没有账号请 注册新账号