Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra: Array into HTML table

Tags:

ruby

sinatra

I am new to Sinatra.

I work currently on a project that is supposed to use a array,

for example: ary = ['a','b','c']

to covert it into an HTML table (possible using an .erb file).

The table should have a single column with as many rows, as there are strings (dynamic).

for example:

  • a
  • b
  • c
  • (any other item of the array)

I don't really have a clue how to do that and I tried code from similar projects, that didn't work. I hope its even possible to do.

like image 797
T.Lange Avatar asked Mar 07 '26 21:03

T.Lange


1 Answers

In the controller do

get '/something' do
  @ary = ['a','b','c']
  erb :'something'
end

In the view named something you can do

<table>
<% @ary.each do |elem| %>
  <tr>
    <td>
      <%= elem %>
    </td>
  </tr>
<% end %>
</table>
like image 123
Ismael Avatar answered Mar 09 '26 13:03

Ismael