user6465508 user6465508 - 5 months ago 15
Ruby Question

Printing the values of the array in html.erb file in rails

Im trying to write code to print the contents of the

@lp_array
array in the html.erb file but it's not printing anything:

<!DOCTYPE html>
<html>
<head>
<title> Blalalala</title>
</head>
<body>
<h1>Please check the items <br><br></h1>

<% @lp_array.each do |lp|
puts lp
end
%>

</body>
</html>


The contents of the array is:

["LPAD736954", "LPAD762667", "LPAD723626", "LPAD737590", "LPAD737593", "LPAD765808", "LPAA550163", "LPAD737675", "LPAD843011", "LPAD723593", "LPAD720060", "LPAD737605", "LPAD731967", "LPAD761385", "LPAD731914", "LPAD721120", "LPAD736970", "LPAD765586", "LPAD765579", "LPAD720104", "LPAD723598", "LPAD779847", "LPAD762699"]


and I'd like to print each element in a new line. How should I print that ?

Answer

Any time that you write ruby in erb you have to remember to use the correct tag. So change your view to this:

<!DOCTYPE html>
<html>
    <head>
        <title> Blalalala</title>
    </head>
    <body>
        <h1>Please check the items <br><br></h1>

        <%- @lp_array.each do |lp| %>
                <%= lp %>
            <%- end %>

    </body>
</html>

notice that the "=" in the erb tag tells the view to actually show the variable. The "-" in the erb tag tells the view not to show the variable. To print each to a new line you can use
or use CSS. for
just add one to the loop like so:

<!DOCTYPE html>
<html>
    <head>
        <title> Blalalala</title>
    </head>
    <body>
        <h1>Please check the items <br><br></h1>

        <%- @lp_array.each do |lp| %>
                <%= lp %><br>
            <%- end %>

    </body>
</html>