Ruby-on-rails 使用 ERB 打印数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13219475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing elements of array using ERB
提问by Keva161
I'm trying to print a simple array defined in my controller into my view with a new line for each element. But what it's doing is printing the whole array on one line.
我正在尝试将在我的控制器中定义的简单数组打印到我的视图中,每个元素都有一个新行。但它所做的是在一行上打印整个数组。
Here's my controller:
这是我的控制器:
class TodosController < ApplicationController
def index
@todo_array = [ "Buy Milk", "Buy Soap", "Pay bill", "Draw Money" ]
end
end
Here's my view:
这是我的观点:
<%= @todo_array.each do |t| %>
<%= puts t %><\br>
<% end %>
Here's the result:
结果如下:
<\br> <\br> <\br> <\br> ["Buy Milk", "Buy Soap", "Pay bill", "Draw Money"]
回答by spike
Erb, the templating engine you're using in your views, has a few different ways of embedding ruby code inside templates.
Erb,您在视图中使用的模板引擎,有几种不同的方式可以将 ruby 代码嵌入到模板中。
When you put code inside <%= %>blocks, erb evaluates the code inside and prints the value of the last statement in the HTML. Since .eachin ruby returns the collection you iterated over, the loop using <%= %>attempts to print a string representation of the entire array.
当您将代码放入<%= %>块中时,erb 会评估里面的代码并打印 HTML 中最后一条语句的值。由于.each在 ruby 中返回您迭代的集合,因此循环使用<%= %>尝试打印整个数组的字符串表示。
When you put code inside <% %>blocks, erb just evaluates the code, not printing anything. This allows you to do conditional statements, loops, or modify variables in the view.
当您将代码放入<% %>块中时,erb 只会评估代码,而不打印任何内容。这允许您在视图中执行条件语句、循环或修改变量。
You can also remove the putsfrom puts t. Erb knows to try to convert the last value it saw inside <%= %>into a string for display.
您还可以puts从puts t. Erb 知道尝试将它在内部看到的最后一个值<%= %>转换为字符串以供显示。
回答by Akshay Vishnoi
Hey why are you putting '=' sign in first line. <% %> are used for telling rails that string under this is ruby code,evaluate it. Where as <%= %> this tells rails that string in these tags is in ruby, evaluate it and print the result in html file too.
嘿,你为什么把 '=' 符号放在第一行。<% %> 用于告诉 rails 这个下的字符串是 ruby 代码,求值。其中 <%= %> 这告诉 rails 这些标签中的字符串在 ruby 中,评估它并将结果也打印在 html 文件中。
Hence try to inspect your code you are writing
因此尝试检查您正在编写的代码
<%=@todo_array.each do |t| %>
while this line is only for iterating over @todo_array hence we wont be in need to print that line. So final code should be
<%=@todo_array.each 做 |t| %> 而这一行仅用于迭代 @todo_array 因此我们不需要打印该行。所以最终的代码应该是
<% @todo_array.each do |t| %>
<%= puts t %>
<% end %>
回答by SporkInventor
Two issues with your view:
你的观点有两个问题:
- you are using "<%=" where you should be using "<%".
- you don't need 'puts'
- 您在应该使用“<%”的地方使用了“<%=”。
- 你不需要“看跌期权”
This should improve your results:
这应该会改善您的结果:
<% @todo_array.each do |t| %>
<%= t %><\br>
<% end %>
I would further consider using some HTML structure to better structure your todo list (instead of using br tag at the end of lines), perhaps an un-ordered list like so:
我会进一步考虑使用一些 HTML 结构来更好地构建您的待办事项列表(而不是在行尾使用 br 标签),也许是一个像这样的无序列表:
<ul>
<% @todo_array.each do |t| %>
<li><%= t %></li>
<% end %>
</ul>
回答by RAJ
Just try:
你试一试:
<%= @todo_array.join('<br />').html_safe %>
<%= @todo_array.join('<br />').html_safe %>
instead of
代替
<%= @todo_array.each do |t| %>
<%= puts t %><\br>
<% end %>
回答by Erez Rabih
Remove the equal sign from your each statement:
从每个语句中删除等号:
<% @todo_array.each do |t| %>
<%= t %><\br>
<% end %>

