为什么结果返回十六进制而不是实际值?

时间:2013-09-07 08:31:43

标签: ruby-on-rails ruby-on-rails-3 railstutorial.org

我正在关注Michael Hartl的ROR教程,出于某种原因,我的用户在浏览器中以十六进制而不是他们的名字返回。这是为什么?

class UsersController < ApplicationController

def index
@users = User.all
end

def new
end


end


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

has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
has_many :followed_users, :through => :relationships, :source => :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name:     "Relationship", :dependent => :destroy

has_many:粉丝,:through =&gt; :reverse_relationships,:source =&gt; :从动

def following?(other_user)
relationships.find_by(followed_id: other_user.id)
end

def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end

def unfollow!(other_user)
relationships.find_by(followed_id: other_user.id).destroy!
end


end

<h1>All Users</h1>

<ul class="users">
<% @users.each do |user| %>
<li>
    <%= link_to user, user %>
</li>
<% end %>
</ul>

<% provide(:title, @user) %>
<div class="row">
<aside class="span4">
    <section>
        <h1>
            <%= @user %>
        </h1>
    </section>
    <section>
    </section>
</aside>
<div class="span8">
    <%= render 'follow_form' if signed_in? %>
</div>
    </div>

1 个答案:

答案 0 :(得分:3)

它不是十六进制,它返回一个对象。尝试在用户表中打印user.name或属性或字段以获取用户名。试试:

<%= link_to user.name, user %>

而不是

<%= link_to user, user %>

<%= @user.name %>

而不是

<%= @user %>

所有用户

<ul class="users">
<% @users.each do |user| %>
<li>
    <%= link_to user.name, user %>
</li>
<% end %>
</ul>

<% provide(:title, @user) %>
<div class="row">
<aside class="span4">
    <section>
        <h1>
            <%= @user.name %>
        </h1>
    </section>
    <section>
    </section>
</aside>