本文介绍了强参数需要多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了一个 JSON 包,例如:

I'm receiving a JSON package like:

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

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

I would like to tell Rails that both point_code and guid are required, not just permitted.

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

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?

推荐答案

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

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

示例:

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

这篇关于强参数需要多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:17