Each.iterator的打印量超出了预期,Ruby on rails 4 [英] Each.iterator prints more than it should, Ruby on rails 4

查看:77
本文介绍了Each.iterator的打印量超出了预期,Ruby on rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html.erb文件中有此文件:

 <%= @ pg_search_documents.each | result | %> 
<%= simple_format(result.content)%>< br>< br>
<%end%>

输出如下:

  nadja kuhn 


Zwischen Nadja Dazwischen


[#< PgSearch :: Document id:17,内容: nadja kuhn,searchable_id:122,searchable_type:事件,created_at: 2016-03-18 22:45:02,updated_at: 2016-03-18 22:45:02> ;, < PgSearch ::文档ID:19,内容: Zwischen Nadja Dazwischen,searchable_id:124,searchable_type: Event,created_at: 2016-03-18 22:45:02,updated_at: 2016-03- 18 22:45:02>]

前两行按预期方式打印,但不知何故

如果我这样写:

 <%= @pg_search_documents。每个|结果| %> 

前两行如预期的那样消失了,但是不知何故仍然是此列表:

  [#< PgSearch :: Document id:17,内容: nadja kuhn,searchable_id: 122,searchable_type:事件,created_at: 2016-03-18 22:45:02,update_at: 2016-03-18 22:45:02> ;,#< PgSearch :: Document id:19 ,内容: Zwischen Nadja Dazwischen,searchable_id:124,searchable_type: Event,created_at: 2016-03-18 22:45:02,updated_at: 2016-03-18 22:45:02> ] 

如果我什么也没写,那么什么也没发生。我该怎么办,第二个列表不会被打印?

解决方案

这是因为您要输出的行您的迭代器:

 <%= @ pg_search_documents.each |结果| %> 

注意多余的 = to_s 在引擎盖下。

 <%@pg_search_documents。每个结果|| %> 

请参见这个问题关于 <%...%> 之间的差异和<%= ...%>


I have this in my html.erb file:

<%= @pg_search_documents.each do |result| %>
    <%= simple_format(result.content) %><br><br>
<% end %>

The output is the following:

nadja kuhn


Zwischen Nadja Dazwischen


[#<PgSearch::Document id: 17, content: "nadja kuhn", searchable_id: 122, searchable_type: "Event", created_at: "2016-03-18 22:45:02", updated_at: "2016-03-18 22:45:02">, #<PgSearch::Document id: 19, content: "Zwischen Nadja Dazwischen", searchable_id: 124, searchable_type: "Event", created_at: "2016-03-18 22:45:02", updated_at: "2016-03-18 22:45:02">] 

The first two lines are printed as expected, but somehow, there comes more, than just the content, namely, the list with the whole entries.

If I write this:

<%= @pg_search_documents.each do |result| %>

The first two lines disappear, as expected, but somehow, there is still this list:

[#<PgSearch::Document id: 17, content: "nadja kuhn", searchable_id: 122, searchable_type: "Event", created_at: "2016-03-18 22:45:02", updated_at: "2016-03-18 22:45:02">, #<PgSearch::Document id: 19, content: "Zwischen Nadja Dazwischen", searchable_id: 124, searchable_type: "Event", created_at: "2016-03-18 22:45:02", updated_at: "2016-03-18 22:45:02">] 

If I write nothing, then nothing happens. What do I need to do, that this second list doesn't get printed?

解决方案

That's because you are outputing the line of your iterator:

<%= @pg_search_documents.each do |result| %>

Notice the extra =, which essentially calls to_s under the hood. Take it out.

<% @pg_search_documents.each do |result| %>

See this question on the difference between <% ... %> and <%= ... %>.

这篇关于Each.iterator的打印量超出了预期,Ruby on rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆