Ruby on rails Rails 4中不需要的哈希打印
我有一个友谊模型正在运行,但是当我在我的用户/显示视图中包含此代码时,我得到了一个不想要的结果:Ruby on rails Rails 4中不需要的哈希打印,ruby-on-rails,ruby-on-rails-4,ruby-2.1,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">] 朋友 戴夫·奥尔森,接受采访 [#] 我不明白为
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">]
朋友
戴夫·奥尔森,接受采访
[#]
我不明白为什么会打印出额外的哈希
以下是我认为的代码:
<h3>Friends</h3>
<%= @user.friendship.each do |friendship| %>
<p><%= friendship.friend.name %>, <%= friendship.status %></p>
<% end %>
朋友
,
用户模型为:
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
class用户
以及我的友谊模型的相关部分:
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
类友谊
我能够消除散列的唯一方法是不运行块。不幸的是,这是行不通的。那么,为什么要打印散列呢?我试图寻找答案,但没有成功。任何帮助都将不胜感激。从
每个
循环中使用的erb脚本中删除等号=
:
<h3>Friends</h3>
<% @user.friendship.each do |friendship| %>
<p><%= friendship.friend.name %>, <%= friendship.status %></p>
<% end %>
朋友
,
您看到不需要的散列的原因是
这个标签将输出一些东西
<%= ... %>
此标签将不可用
<% ... %>