gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:42:32 26 4
gpt4 key购买 nike

我正在关注 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com