How to present an array in ruby-rails view template?

I represent a variable in rails view.

<%= @data.matrix %>

which prints a multidimensional array in a template as follows:

[[":23", ":12"],[ ":56", ":12"],[":21", ":23"]]

As I can present the above data in a table format, that it will actually look like a matrix on the page view.htmlto better read the rows of values ​​and colum. We also have information about templates @data.rowand @data.col.

This is more of a question HTML, but is there a way to do this using syntax rails view?

+4
source share
2 answers

You can do something like:

<table>
  <% JSON.parse(@data.matrix).each do |tuple| %>
    <tr>
      <% tupel.each do |value| %>
        <td><%= value %></td>
      <% end %>
    </tr>
  <% end %>
</table>
+2
source

You can do something like multiple arrays,

<table>
  <% @data.matrix.flatten.each do |value| %>
    <tr>
      <td><%= value %></td>
    </tr>
  <% end %>
</table>
0
source

All Articles