0

如何公开 VehicleDetails 的修订属性,该属性不会出现在GET请求中,但在PATCH/POST上是强制性的(仅写入操作)?

class VehicleDetails < Grape::Entity
  expose :id
  expose :name
  expose :type
  expose :revision
end
4

1 回答 1

0
vehicle = { id: 1, name: 'LADA', type: 'washbowl', revision: 15 }

class VehicleDetails < Grape::Entity
  expose :id
  expose :name
  expose :type
  expose :revision, if: lambda { |vehicle, options| options[:show_rev] }
end

VehicleDetails.represent(vehicle, show_rev: true).as_json
# => {:id=>1, :name=>"LADA", :type=>"washbowl", :revision=>15}

VehicleDetails.represent(vehicle).as_json
# => {:id=>1, :name=>"LADA", :type=>"washbowl"}

VehicleDetails.represent(vehicle, show_rev: false).as_json
# => {:id=>1, :name=>"LADA", :type=>"washbowl"}

# # or
# present vehicle with: VehicleDetails, show_rev: true
于 2017-12-28T16:26:32.077 回答