Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

725
Visualizações
How to present JSON information in a html.erb file (Ruby)?

I am new to Ruby/Rails and I've been given a task to make a Ruby site (.html.erb) look a bit nicer. One of the things requested was to present information on the site, which is currently shown as JSON, as nice-looking html. The line of html.erb is as follows:

<%= @buyer.generate_profile.inspect %><br>

and will display the information it receives on the site as JSON. What can I do to parse through the JSON and make it so that the site will display the information as proper html?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You might want to check out JSON.pretty_generate. You could display the result in a code block like:

<%# If your data is a JSON string, convert it to a hash %>
<% hash = JSON.parse(@buyer.generate_profile) %>
<pre>
  <code>
    <%= JSON.pretty_generate(hash) %>
  </code>
</pre>
over 4 years ago · Santiago Trujillo Relatório

0

So the generate_profile method returns a Hash object and calling inspect on it will create string output of the Hash syntax in Ruby, so the result looks like your comment.

If you wanted it to look like JSON, the simplest way would be to use the to_json method like this

<%= @buyer.generate_profile.to_json %>

but honestly, it's not much better. And even if you were to look into methods that can present it with proper line breaks, etc. that wouldn't improve it much either imo.

I would recommend you learn how to iterate over the keys and values in a Hash, because that would allow you to create a custom HTML layout that could look however you wanted it to.

As an example, I'll show you how you could create a simple table based on the data from your comment

<table>
  <thead>
  <tr>
    <th>Key</th>
    <th>XX</th>
    <th>YY</th>
    <th>ZZ</th>    
  </tr>
  </thead>
  <tbody>
  <% @buyer.generate_profile.each_pair do |key, sub_hash| %>
    <tr>
      <td><%= key %></td>
      <td><%= sub_hash[:xx] %></td>
      <td><%= sub_hash[:yy] %></td>
      <td><%= sub_hash[:zz] %></td>
    </tr>
  <% end %>
  </tbody>
</table>

The main thing here is the use of each_pair which is a method that iterates over each key and value of the hash. In your case it sounds like the values of the first hash are sub-hashes, hence my use of sub_hash as the block argument.

You could technically use each_pair on the sub-hashes as well, but I'm guessing they all have the same keys, that's why I manually added columns and cells for XX, YY, ZZ in combination with sub_hash[:xx], etc.

Once you get a hang of how iterating over Hashes and arrays work, then I would recommend scrapping the table design and instead look into more modern web design approaches like Flex and CSS-Grid, but one thing at a time :)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda