简体   繁体   English

是否可以在 Rails 中要求多个强参数?

[英]Is it possible to require multiple strong params in Rails?

I'm trying to do some server-side validation for a form in Rails, and I would like to require specific fields to have a value.我正在尝试对 Rails 中的表单进行一些服务器端验证,我想要求特定字段具有值。 Here are the params in my controller:这是我的控制器中的参数:

private
def post_params
  params.require(:post).permit(:title, :text, :slug)
end

The problem is that the server merely permits these params — it doesn't require them, so it's possible to submit a completely blank form.问题是服务器只允许这些参数——它不需要它们,所以可以提交一个完全空白的表单。

My understanding is that params.require only accepts one argument, and I've tried getting around this with no success:我的理解是params.require只接受一个参数,我尝试解决这个问题但没有成功:

private
def post_params
  params.require(:post, :title, :text, :slug)
end
# => wrong number of arguments (4 for 1)

private
def post_params
  all_params = [:post, :title, :text, :slug]
  params.require(all_params)
end
# => param is missing or the value is empty: [:post, :title, :text, :slug]

private
def post_params
  params.require(:post).require(:title).require(:text).require(:slug)
end
# => private method `require' called for "test thing":String

Is there any way to require multiple strong params?有没有办法需要多个强参数?

Strong Parameters are used for mass assignment.强参数用于质量分配。 What does mass assignment means is that when you submit a form multiple values are submitted ie, multiple attributes of a model so you add them in the params.require(:post).permit .批量分配的含义是,当您提交表单时,会提交多个值,即模型的多个属性,因此您将它们添加到params.require(:post).permit You are seeing the word permit which means this attributes are permitted to be mass assigned.您看到了permit一词,这意味着允许批量分配此属性。 Now suppose you are writing this in controller:现在假设你在控制器中写这个:

def post_params
  params.require(:post).permit(:title, :text, :slug)
end

and in your params suppose you have:在你的参数中假设你有:

{"post"=>{"title"=>'Test',"text"=>"Text test","slug"=>"any random name","type"=>"article"}}

Now if you do:现在如果你这样做:

@post = Post.new(post_params)
@post.save

It will give you unpermitted parameters :type because you have not whitelisted the type parameter.它会给你unpermitted parameters :type因为你没有将type参数列入白名单。 This is given for security reasons so that you only allow attributes that needs to be updated by user from the view are permitted.这是出于安全原因给出的,因此您只允许需要由用户从视图更新的属性。 And the strong parameters are introduced from rails 4 onwards.并且强参数是从rails 4开始引入的。

Now the thing you need to require these parameters that the user fills them so this needs to be done in the model like this:现在您需要要求用户填写这些参数,因此这需要在模型中完成,如下所示:

class Post < ActiveRecord::Base
  validates_presence_of :title, :text, :slug
end

This is a validation which will make the users enter the title, text and slug necessarily.这是一种验证,将使用户必须输入title, text and slug There are more type of validations and some custom validations too which can be written according to the needs.还有更多类型的验证和一些自定义验证,可以根据需要编写。

More Info:更多信息:

Strong Parameters强参数

Validations验证

Hope this helps希望这可以帮助

Strong params is not (and cannot) be used for validating the presence of attributes.强参数不是(也不能)用于验证属性的存在。 As @Deep said you use the validate presence for that.正如@Deep 所说,您为此使用了验证存在。

The academic question remains how to require multiple strong params, the word 'params' is misleading here as the 'require' part specifies the key of the attribute hash.学术问题仍然是如何要求多个强参数,“params”一词在这里具有误导性,因为“require”部分指定了属性哈希的键。 The reason require only accepts one is because typically a form is submitted with an attribute hash representing a single object - nesting aside. require 只接受一个的原因是因为通常提交的表单带有表示单个对象的属性哈希 - 嵌套在一边。

In theory though, if your params hash looked contained these key/value pairs:但理论上,如果您的 params 哈希看起来包含这些键/值对:

... this: { name: 'this', number: 1 }, that: { color: 'red', size: 'big'} ...

You might legitimately want to do requires for both this: and that: and permit attributes for them independently, if so you could do this:您可能合法地想要对 this: 和 that: 执行 requires 并独立允许它们的属性,如果是这样,您可以这样做:

def this_and_that_params
  { this: params.require(:this).permit(:name), that: params.require(:that).permit(:color) }
end

which would return:这将返回:

... this: { name: 'this' }, that: { color: 'red' } ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM