Rails输出对象哈希

Jam*_*mes 1 ruby-on-rails erb

这是有趣的.我有一些看起来像这样的视图代码:

    <%= @cause.updates.each do |update| %>
      <div class="streamComment group">

      <img class="userPhoto" src="<%= update.user.avatar.url %>">

      <p class="userComment"><%= update.update_text %></p>
      </div>
    <% end %>
Run Code Online (Sandbox Code Playgroud)

在段落标记的末尾和div标记的结尾之间,rails在输出更新对象的散列时,即"<#Update 0x6993934ksf>",当视图中不存在任何内容时.可能是什么导致了这个?

Jim*_*lle 5

您在<%=%>处使用<%=%>.由于每个都返回它迭代的对象,所以一旦完成对更新的迭代,就会返回更新并输出到HTML

<% @cause.updates.each do |update| # remove the = at the beginning of this line %>
  <div class="streamComment group">

  <img class="userPhoto" src="<%= update.user.avatar.url %>">

  <p class="userComment"><%= update.update_text %></p>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)