我试图编写代码来打印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>
数组的内容是:
["LPAD736954", "LPAD762667", "LPAD723626", "LPAD737590", "LPAD737593", "LPAD765808", "LPAA550163", "LPAD737675", "LPAD843011", "LPAD723593", "LPAD720060", "LPAD737605", "LPAD731967", "LPAD761385", "LPAD731914", "LPAD721120", "LPAD736970", "LPAD765586", "LPAD765579", "LPAD720104", "LPAD723598", "LPAD779847", "LPAD762699"]
我想在一个新行中打印每个元素。我该如何打印?
答案 0 :(得分:3)
任何时候你在erb中写ruby你都要记得使用正确的标签。所以改变你的看法:
<!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>
注意erb标记中的“=”告诉视图实际显示变量。 erb标记中的“ - ”表示视图不显示变量。要将每个打印到新行,您可以使用
或使用CSS。 for zh只是像这样添加一个循环:
<!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>