Ruby on rails 在微操和评论中显示链接到作者的链接';她的个人资料?
我创建了一个Micropost模型,该模型具有以下属性:Ruby on rails 在微操和评论中显示链接到作者的链接';她的个人资料?,ruby-on-rails,Ruby On Rails,我创建了一个Micropost模型,该模型具有以下属性: <Micropost id: 1, content: "test", user_id: 1, created_at: "2012-01-25 15:34:30", updated_at: "2012-01-29 11:07:53", title: "asdasdad"> <User id: 1, email: "alex@gmail.com", username: nil, etc...> <Commen
<Micropost id: 1, content: "test", user_id: 1, created_at: "2012-01-25 15:34:30", updated_at: "2012-01-29 11:07:53", title: "asdasdad">
<User id: 1, email: "alex@gmail.com", username: nil, etc...>
<Comment id: 1, content: "sdf", micropost_id: 1, user_id: nil, created_at: "2012-01-29 11:10:42", updated_at: "2012-01-29 11:10:42">
有什么建议吗?创建指向您可以使用的用户的链接
<%= link_to comment.user.username, comment.user %>
或者因为您在microspost
和Comment
# Link:
=link_to "User profile", (@micropost.user)
# Name of the author
=@micropost.user.username
谢谢你的回答。请检查我的编辑我包括我的模型。但出于某种原因,我得到了这样的结果:
未定义的方法
username',用于nil:NilClass`奇怪但只有注释。用户
工作(显示:#
)。是因为我需要在comments表中设置外键(例如user_username)?可能是因为您没有在microspost
实例中设置user_id。检查您的@micropost
,查看用户id是否为空。如果为空,请检查您的控制器以确认您使用的是类似于@user.microsposts.create(…)
或类似的东西。@timbrandes抱歉,我不太清楚您的意思。请检查我的编辑我添加了我的micropost控制器的一部分(显然,我确实有类似于:@user.microposts.new
)的内容。您得到错误是因为您的用户名在示例代码中为零:-如果您只使用comment.user,您得到了ruby对象..出于某种原因,comment.user.username
抛出错误:undefined method
username'表示nil:NilClass`
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
has_many :microposts
has_many :comments
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
user
else # Create a user with a stub password.
User.create!(:email => data.email, :password => Devise.friendly_token[0,20])
end
end
end
class Micropost < ActiveRecord::Base
attr_accessible :title, :content
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :micropost
belongs_to :user
end
def show
@micropost = Micropost.find(params[:id])
end
def new
@micropost = Micropost.new
end
def create
@user = current_user
@micropost = @user.microposts.new(params[:micropost])
@micropost.save
redirect_to @micropost
end
<%= link_to comment.user.username, comment.user %>
class User < ActiveRecord::Base
validates_presence_of :username #username should obviously not allow nil values
has_many :microposts
has_many :comments
end
class MicroPost < ActiveRecord::Base
belongs_to :user
end
class Comment < ActiveRecord::Base
belongs_to :user
end
# Link:
=link_to "User profile", user_path(comment.user)
# Name of the author
=comment.user.username
# Link:
=link_to "User profile", (@micropost.user)
# Name of the author
=@micropost.user.username