问题
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