简体   繁体   English

在 rails 的 html.erb 文件中打印数组的值

[英]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:我正在尝试编写代码来打印 html.erb 文件中@lp_array数组的内容,但它没有打印任何内容:

<!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 ?我应该如何打印?

Any time that you write ruby in erb you have to remember to use the correct tag.任何时候在 erb 中编写 ruby​​ 时,您都必须记住使用正确的标签。 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.请注意,erb 标记中的“=”告诉视图实际显示变量。 The "-" in the erb tag tells the view not to show the variable. erb 标签中的“-”告诉视图不要显示变量。 To print each to a new line you can use <br> or use CSS.要将每个打印到新行,您可以使用 <br> 或使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM