Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

732
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda