Rails 4中的不需要的哈希打印

Dav*_*son 0 ruby-on-rails ruby-on-rails-4 ruby-2.1

我有一个友好的模型,但是当我在我的用户/节目视图中包含此代码时,我得到了这个不需要的结果:

Friends
Dave Olson, accepted

[#<Friendship id: 74, user_id: 1, friend_id: 2, status: "accepted", created_at: "2014-03-27 03:54:08", updated_at: "2014-03-27 03:54:09">]
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚为什么额外的哈希打印出来.

这是我的观点中的代码:

<h3>Friends</h3>
<%= @user.friendship.each do |friendship| %>
<p><%= friendship.friend.name %>, <%= friendship.status %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

用户模型是:

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

         has_many :items
         has_many :friendship
end
Run Code Online (Sandbox Code Playgroud)

以及我的友谊模型的相关部分:

class Friendship < ActiveRecord::Base

    belongs_to :user
    belongs_to :friend, class_name: "User", foreign_key: "friend_id"

    validates_presence_of :user_id, :friend_id
....more code
end
Run Code Online (Sandbox Code Playgroud)

我能够消除哈希的唯一方法是不运行块.不幸的是,这不会起作用.那么,为什么哈希打印出来?我试过寻找答案,但没有取得任何成功.任何帮助将不胜感激.

vee*_*vee 5

=从each循环中使用的erb scriptlet中删除等号:

<h3>Friends</h3>
<% @user.friendship.each do |friendship| %>
  <p><%= friendship.friend.name %>, <%= friendship.status %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

您看到不需要的哈希的原因是因为<%=与<%用于打印输出相反.因此,<%= @user.friendship.each...打印该each块返回的结果.