Ruby-on-rails 强参数需要多个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22487878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:27:19  来源:igfitidea点击:

Strong parameters require multiple

ruby-on-railsrubyruby-on-rails-4

提问by Rob

I'm receiving a JSON package like:

我收到一个 JSON 包,如:

{
  "point_code" : { "guid" : "f6a0805a-3404-403c-8af3-bfddf9d334f2" }
}

I would like to tell Rails that both point_codeand guidare required, not just permitted.

我想告诉 Rails,point_codeguid都是必需的,而不仅仅是允许的。

This code seems to work but I don't think it's good practice since it returns a string, not the full object:

这段代码似乎有效,但我认为这不是一个好习惯,因为它返回一个字符串,而不是完整的对象:

params.require(:point_code).require(:guid)

Any ideas how I could do this?

任何想法我怎么能做到这一点?

采纳答案by sarunw

I had a similar need and what I did was

我有类似的需求,我所做的是

def point_code_params
  params.require(:point_code).require(:guid) # for check require params
  params.require(:point_code).permit(:guid) # for using where hash needed
end

Example:

例子:

def create
  @point_code = PointCode.new(point_code_params)
end

回答by raveren

As of 2015 (RoR 5.0+) you can pass in an array of keys to the requiremethod in rails:

截至 2015 年(RoR 5.0+),您可以将一组键传递给requirerails 中的方法:

params.require([:point_code, :guid])

params.require([:point_code, :guid])

http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-require

http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-require

回答by EdvardM

OK, ain't pretty but should do the trick. Assume you have params :foo, :bar and :baf you'd like to require all for a Thing. You could say

好吧,不漂亮,但应该能解决问题。假设你有参数 :foo, :bar 和 :baf 你想要所有的东西。你可以说

def thing_params
  [:foo, :bar, :baf].each_with_object(params) do |key, obj|
    obj.require(key)
  end
end

each_with_objectreturns obj, which is initialized to be params. Using the same params obj you require each of those keys in turn, and return finally the object. Not pretty, but works for me.

each_with_object返回 obj,它被初始化为 params。使用相同的 params obj 依次需要每个键,并最终返回对象。不漂亮,但对我有用。

回答by Pere Joan Martorell

The solution proposed in Rails documentation (https://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-require) is:

Rails 文档(https://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-require)中提出的解决方案是:

def point_code
  params.require(:point_code).permit(:guid).tap do |point_code_params|
    point_code_params.require(:guid) # SAFER
  end
end

回答by Ethan Dowler

Although a lot of these answers show you how to get what you're asking for, I would like to add to these answers and suggest that you use a before_action. If the required params are missing, requirewill automatically return a 400 error

尽管这些答案中有很多向您展示了如何获得您所要求的内容,但我想添加到这些答案中并建议您使用before_action. 如果缺少所需的参数,require将自动返回 400 错误

class SomeController < ApplicationController
  before_action :ensure_valid_params, only: :index

  def index
    # do your stuff
  end

private

  def ensure_valid_params
    params.require(:some_required_param)
  end
end

回答by Nate Flink

This question came up in my google search for a different case ie, when using a "multiple: true" as in:

这个问题在我的谷歌搜索中出现了一个不同的案例,即当使用“multiple: true”时:

<%= form.file_field :asset, multiple: true %>

Which is a totally different case than the question. However, in the interest of helping out here is a working example in Rails 5+ of that:

这与问题完全不同。但是,为了在这里提供帮助,这是 Rails 5+ 中的一个工作示例:

form_params = params.require(:my_profile).permit({:my_photos => []})

回答by usha

requiretakes one parameter. So there is no way you can pass in multiple keys unless you override the requiremethod. You can achieve what you want with some additional logic in your action:

require接受一个参数。因此,除非您覆盖该require方法,否则您无法传入多个键。您可以在操作中使用一些额外的逻辑来实现您想要的:

def action
  raise ActionController::ParameterMissing.new("param not found: point_code") if point_params[:point_code].blank?
  raise ActionController::ParameterMissing.new("param not found: guid") if point_params[:point_code][:guid].blank?

  <do your stuff>
end

def point_params
  params.permit(point_code: :guid)
end