i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?
my controller
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",
},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]
You will have to understand about JSON builders,
start here https://www.ruby-toolbox.com/categories/API_Builders
Choose one and get along .
I have used Jbuilder quite a few times
create json-jbuilder view
app/[controller]/show.json.jbuilder
json.id address.id
json.user_id address.user_id
json.city address.city
...
your controller action
def show
@address = Address.where(user_id:params[:id])
end
for more details you may read jBuilder documentation https://github.com/rails/jbuilder
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.