anquegi
anquegi

Reputation: 11522

Strong parameters with json array

I allready read this anwer how to permit an array with strong parameters but I cannot figure this situation using params.permit, from a byebug session using rails 4.2:

how can I extract the comments parameters?

(byebug) params
{"comments"=>"[{\"comment\":\"ndjsnjakldnfljkasdbfhjae\",\"date\":\"2017-07-20 17:14:38\"}]", "format"=>:json, "controller"=>"airis/observations", "action"=>"create", "id"=>"13534543543"}
(byebug) params.permit(:id)
[2017-07-20 19:14:43 +0200] [f2b3d7c8-c84a-43a5-b48f-0f634be49dc7] Unpermitted parameters: comments, format
{"id"=>"13534543543"}
(byebug) params.permit(:comments)
[2017-07-20 19:14:43 +0200] [f2b3d7c8-c84a-43a5-b48f-0f634be49dc7] Unpermitted parameters: format, id
{"comments"=>"[{\"comment\":\"ndjsnjakldnfljkasdbfhjae\",\"date\":\"2017-07-20 17:14:38\"}]"}
(byebug) params.permit(comments: [:comment, :date])
[2017-07-20 19:14:43 +0200] [f2b3d7c8-c84a-43a5-b48f-0f634be49dc7] Unpermitted parameters: format, id
{"comments"=>nil}
(byebug) params.permit(:id, comments: [:comment, :date])
[2017-07-20 19:14:43 +0200] [f2b3d7c8-c84a-43a5-b48f-0f634be49dc7] Unpermitted parameter: format
{"id"=>"13534543543", "comments"=>nil}

or even with this:

(byebug) params.permit( :comments => [:comment, :date])
[2017-07-20 19:14:43 +0200] [f2b3d7c8-c84a-43a5-b48f-0f634be49dc7] Unpermitted parameters: format, id
{"comments"=>nil}

Upvotes: 1

Views: 1215

Answers (1)

jvillian
jvillian

Reputation: 20263

Give this a try:

2.3.1 :034 > JSON.parse(params.permit(:comments)[:comments])
Unpermitted parameters: format, id
 => [{"comment"=>"ndjsnjakldnfljkasdbfhjae", "date"=>"2017-07-20 17:14:38"}]  

params.permit(:comments) returns the params hash with only the "comments" k,v pair (dropping the unpermitted parameters, format and id):

2.3.1 :035 > params.permit(:comments)
Unpermitted parameters: format, id
 => {"comments"=>"[{\"comment\":\"ndjsnjakldnfljkasdbfhjae\",\"date\":\"2017-07-20 17:14:38\"}]"} 

params.permit(:comments)[:comments] selects the value for the :comments key from the hash:

2.3.1 :036 > params.permit(:comments)[:comments]
Unpermitted parameters: format, id
 => "[{\"comment\":\"ndjsnjakldnfljkasdbfhjae\",\"date\":\"2017-07-20 17:14:38\"}]" 

JSON.parse(params.permit(:comments)[:comments]) converts the string to a proper ruby array.

From there, you can do as you like with the ruby array object.

If you want both :comments and :id, then just:

params.permit(:comments, :id)
Unpermitted parameter: format
 => {"comments"=>"[{\"comment\":\"ndjsnjakldnfljkasdbfhjae\",\"date\":\"2017-07-20 17:14:38\"}]", "id"=>"13534543543"} 

To get the individual parts:

2.3.1 :038 > params.permit(:comments, :id)[:comments]
Unpermitted parameter: format
 => "[{\"comment\":\"ndjsnjakldnfljkasdbfhjae\",\"date\":\"2017-07-20 17:14:38\"}]" 

and:

2.3.1 :039 > params.permit(:comments, :id)[:id]
Unpermitted parameter: format
 => "13534543543" 

You probably want to wrap it in something like:

def clean_params
  @clean_params ||= params.permit(:comments, :id)
end

So you can do clean_params[:comments] and clean_params[:id].

Upvotes: 3

Related Questions