首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在微博和评论中显示链接,链接到作者的个人资料?

在微博和评论中显示链接,链接到作者的个人资料?
EN

Stack Overflow用户
提问于 2012-01-30 09:46:58
回答 2查看 810关注 0票数 1

我创建了一个具有以下属性的Micropost模型:

代码语言:javascript
复制
<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">

具有以下属性的用户模型:

代码语言:javascript
复制
<User id: 1, email: "alex@gmail.com", username: nil, etc...>

以及具有以下属性的注释模型:

代码语言:javascript
复制
<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">

到目前为止,我只完成了这一点:

micropost

  • Display的作者
  • 显示用户名--微博作者的ID --我不知道如何创建指向作者配置文件的链接(例如,mysite.com/users/1).
  • I不检索注释作者的姓名和指向他/她的概要文件

F 216的链接)

编辑:

型号:

user.rb:

代码语言:javascript
复制
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

micropost.rb:

代码语言:javascript
复制
class Micropost < ActiveRecord::Base
  attr_accessible :title, :content

  belongs_to :user
  has_many :comments
end

comment.rb:

代码语言:javascript
复制
class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id

  belongs_to :micropost
  belongs_to :user
end

微站控制器:

controllers/microposts.rb

代码语言:javascript
复制
  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

有什么建议来做到这一点吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-30 10:01:34

若要链接到您可以使用的用户,请执行以下操作

代码语言:javascript
复制
<%= link_to comment.user.username, comment.user %>

一般来说,"Rails魔术“的一部分是,如果您正确地设置了一个关联,您就可以通过点符号访问相关的对象。这意味着,您不需要说comment.user_id,而是直接选择关联的用户对象,例如comment.user.usernamecomment.user.email .你的想法是:)

为了达到这个目的,你应该建立这样的模型:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2012-01-30 10:03:02

代码语言:javascript
复制
# Link:
=link_to "User profile", user_path(comment.user)

# Name of the author
=comment.user.username

或者因为user_id在MicropostComment中都有

代码语言:javascript
复制
# Link:
=link_to "User profile", (@micropost.user)

# Name of the author
=@micropost.user.username
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9061954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档