I am new to rails API. I am getting "TypeError No implicit conversion of Symbol into Integer" in rails server console for the following syntax.
def show
@product = Product.find(params[:id])
render json: @product.to_json(:include =>
{ :items =>
{ :only =>
[:id,
:description,
:item_name,
:item_code,
:display_tag,
:price,
{:include =>
:item_images [:id,
:image_url]
}
]
}
}
)
end
It should show the array of item_images as well for items while requesting the URL(e.g http://localhost:4000/products/1) my JSON is like this:
{
"id": 1,
"product_name": "Foundation",
"description": "test",
"image_file_name": "text.jpg",
"image_content_type": "image/jpeg",
"image_file_size": 341279,
"image_updated_at": "2018-10-24T09:28:49.000Z",
"image_url": "http://localhost:3000/system/products/1/original/text.jpg",
"created_at": "2018-08-28T14:50:29.000Z",
"updated_at": "2018-10-24T09:28:50.000Z",
"items": [
{
"id": 6,
"item_name": "LOTUS WHITE GLOW COMPAC POWDER",
"item_code": "SF264",
"display_tag": "New",
"description": "SKIN WHITENING AND BRIGHTENING NOURISHING\r\nSUITABLE FOR ALL SKIN TYPES\r\nULTRA-FINE SILTY FORMULA\r\nSILKY SMOOTH AND DELICATE TEXTURE\r\nCONTAINS NATURAL PLANT INGREDIENTS CAN BLOCK THE INSIDE DARK",
"price": "1250.0"
},
{
"id": 7,
"item_name": "Test",
"item_code": "100",
"display_tag": "New",
"description": "lorem ipsum",
"price": "200.0"
}
]}
Any help?
Look at:
{:include => :item_images [:id, :image_url]}
:item_images [:id,
isn't right. You probably mean something like:
{:include => :item_images => {:only => [:id, :image_url]}}
Also that include is also in the wrong spot I believe. :include should be another key like :only is in that hash (right now :include is in the :only array). So this should work:
def show
@product = Product.find(params[:id])
render json: @product.to_json(:include =>
{ :items =>
{ :only =>
[:id,
:description,
:item_name,
:item_code,
:display_tag,
:price
],
:include =>
{
:item_images => {:only => [:id, :image_url]}
}
}
}
)
end