I know I am missing something probably very obvious. I am trying to iterate over a JSON array in my Rails5 view. I tried several things but can't seem to get the proper items to render. The below code renders {"name"=>"Large Plaque", "price"=>"2500"} {"name"=>"Small Plaque", "price"=>"1500"} to my view.
config/plaque_data.json
{
"products": [
{
"name": "Large Plaque",
"price": "2500"
},
{
"name": "Small Plaque",
"price": "1500"
}
]
}
controllers/plaqueorders_controller.rb
...
def new
@plaqy = JSON.load File.read('config/plaque_data.json')
end
...
views/plaqueorders/new.html.erb
...
<% @plaqy['products'].each do |k, v| { name: k, price: v } %>
<%= k %>
<% end %>
...
JSON.load(File.read('config/plaque_data.json'))? (in your irb console for example)=> {"products"=>[{"name"=>"Large Plaque", "price"=>"2500"}, {"name"=>"Small Plaque", "price"=>"1500"}]}Rails.root.join('config/plaque_data.json').plaque_data.jsonfile is not meant to change, you might want to load it at the server start and cache it instead of reading it every time this action is hit