Each.iterator prints more than it should, Ruby on rails 4 [duplicate]

陌路散爱 提交于 2019-12-12 05:58:08

问题


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?


回答1:


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 <%= ... %>.



来源:https://stackoverflow.com/questions/36095811/each-iterator-prints-more-than-it-should-ruby-on-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!