3

I'm trying to display json in a view that I've put in this variable '@yearbook':

def list
        file = File.open("#{Rails.root}/lib/service.json", "r")
        service = file
        service = service.read
        service = JSON.parse(service)
        @yearbook = service
    end

In my view file, I go through every '@yearbook' element and simply display it like so:

<% @yearbook.each do |item| %>
    <tr>
      <p><%= item %></p>
    </tr>

And this is what I get from the view on my browser (which is normal):

{"name"=>"...", "description"=>"..."}
{"name"=>"...", "description"=>"..."}
...

Now I don't understand how I can specify to the view to display me the "name" and "description" attributes instead of the formatted json object, making it look more like this:

name: ..., description: ...

name: ..., description: ... and so on...

Hope it's clear, cheers.

0

1 Answer 1

1

How about something like below :

<% @yearbook.each do |item| %>
  <tr>
    <p>
      <%= "name:" "#{item["name"]}" %>
      <%= "description:" "#{item["description"]}" %>
    </p>
  </tr>
<% end %>
Sign up to request clarification or add additional context in comments.

4 Comments

Please upvote and accept my answer if it solves your problem, Thanks.
Thanks for answer, the exact code you gave me didn't work, but writing: <p><%= item["name"] %></p> (same for description) showed them which solves my problem.
@HCKRMVT , could you upvote if so my answer helped, i dint mean to copy paste it was my assumption per your question
refer updated answer semicolon ":" should be inside "name", copy paste this answer , it should work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.