Rails输出对象哈希 [英] Rails outputting object hash
本文介绍了Rails输出对象哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这很有趣。我有一些类似于以下的视图代码:
This is interesting. I have some view code that looks like this:
<%= @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 %>
在段落标记的末尾与div标记的末尾之间,rails输出的是当视图中不存在任何更新对象时,即为< #Update 0x6993934ksf>。
Between the end of the paragraph tag and the end of the div tag, rails is outputting the hash of the update object i.e. "<#Update 0x6993934ksf>" when nothing exists there in the view. What could be causing this?
推荐答案
您在需要<%=%>的地方使用<%=%>。由于每个对象都返回迭代的对象,因此,完成更新迭代后,更新将返回并输出到HTML
Your using <%= %> where you need <% %>. Since each returns the object it iterated over, once you are done iterating over updates, updates is returned and output to the 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 %>
这篇关于Rails输出对象哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文