Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm receiving a JSON package like:

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

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?

question from:https://stackoverflow.com/questions/22487878/strong-parameters-require-multiple

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
328 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...