Grape :: Entities,如何仅为写入而公开属性

问题描述 投票:0回答:1

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

class VehicleDetails < Grape::Entity
  expose :id
  expose :name
  expose :type
  expose :revision
end
ruby-on-rails ruby grape-api grape-entity
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
© www.soinside.com 2019 - 2024. All rights reserved.