nifCody
nifCody

Reputation: 2444

Read the JSON array value in the rails controller

I have try to getting the params via post method

POST /api/test HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 945ce038-4bf1-ed50-afcb-cc715cf3a3fc

{
    "service_requests": [{"service_id":2},{"service_id" : 3}]
}

In the controller I have test with a method and print like this. But I could not able to print this. How do I print all the values in for each or for loop from this Json Array of JSON Objects

def test
   render :html => params[:service_requests][0].service_id
end

Upvotes: 1

Views: 812

Answers (1)

Rahul
Rahul

Reputation: 229

Please try this

     render :html => params[:service_requests].map{|s| s["service_id"]}

Upvotes: 2

Related Questions