新手问题 Grape 的 params 如何 剔除一个不需要的 param

u1450154824 · April 13, 2016 · Last by creeek replied at May 11, 2016 · 2191 hits

exactly_one_of 又应该怎么用

如果 我希望有个特定的参数我想保护起来,不想外部传入,应该怎么做

比如 except_of 之类的?

#1 楼 @cysh 人家问的是 grape,不是 rails

#2 楼 @alucardpj 标题改过吧。。?

Grape 不是可以描述参数吗?

找到了,在 activesuppoert 里,

class Hash
  # Returns a hash that includes everything but the given keys.
  #   hash = { a: true, b: false, c: nil}
  #   hash.except(:c) # => { a: true, b: false}
  #   hash # => { a: true, b: false, c: nil}
  #
  # This is useful for limiting a set of parameters to everything but a few known toggles:
  #   @person.update(params[:person].except(:admin))
  def except(*keys)
    dup.except!(*keys)
  end

  # Replaces the hash without the given keys.
  #   hash = { a: true, b: false, c: nil}
  #   hash.except!(:c) # => { a: true, b: false}
  #   hash # => { a: true, b: false }
  def except!(*keys)
    keys.each { |key| delete(key) }
    self
  end
end

如果是在 API 级别,可以使用 params do ... end 定义哪些参数的格式,通过 declared(params) 来使用格式化的参数

You need to Sign in before reply, if you don't have an account, please Sign up first.