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

标签 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 :followers, :through => :reverse_relationships, :source => :follower

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>

最佳答案

它不是十六进制,它返回一个对象。尝试打印 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>

关于ruby-on-rails - 为什么结果返回十六进制而不是实际值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18671429/

相关文章:

ruby-on-rails - 匿名 Controller 的 Rspec stubing View

ruby-on-rails-3 - 跨子域的 rails 3 session 在 Internet Explorer 中不起作用

ruby-on-rails - Rails损坏- bundle 不起作用-ovirt-engine-sdk-错误: Failed to build gem native extension. Cloud9 AWS

javascript - 在 Rails 和 Javascript 中转义 "

ruby-on-rails-3 - Heroku REST API - 有吗?

ruby-on-rails - ZenTest 4.9.3 显示为无效的 gemspec

ruby-on-rails - Hartl 的教程 - 第 5.3 章布局链接

ruby-on-rails - Capistrano 当前应用路径

ruby-on-rails - PG::UndefinedColumn:如果同一数据库用于在 Rails 中相互连接的两个不同项目,则出现错误

ruby-on-rails - 首先 RSPEC 测试,确保值为 1 或 -1