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

时间:2012-01-30 09:46:58

标签: 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...>

以及具有以下属性的Comment模型:

<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">

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

  • 显示微博作者的用户名
  • 显示微博评论作者的 ID

    <h2>Micropost Index</h2>
    <% @microposts.each do |micropost| %>
    
    <%= micropost.title %></td>
    <%= micropost.content %></td>
    <%= link_to 'Show', micropost %></td>
    <%= link_to 'Edit', edit_micropost_path(micropost) %></td>
    <%= link_to 'Destroy', micropost, confirm: 'Are you sure?', method: :delete %>     
    
       
    <h2>Comments</h2>
    
    <% @micropost.comments.each do |comment| %>
      <p>
        <b>Comment:</b>
        <%= comment.content %>
      </p>
      <p>
        <b>Commenter</b>
        <%= comment.user_id %>
      </p>
    <% end %>
    
  • 我不知道如何创建作者个人资料的链接(例如mysite.com/users/1)。

  • 我不知道如何检索评论作者的姓名和他/她的个人资料的链接

修改

型号:

user.rb:

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:

class Micropost < ActiveRecord::Base
  attr_accessible :title, :content

  belongs_to :user
  has_many :comments
end

comment.rb:

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id

  belongs_to :micropost
  belongs_to :user
end

Micropost控制器:

控制器/ microposts.rb

  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

有任何建议可以实现这一目标吗?

2 个答案:

答案 0 :(得分:2)

要建立用户链接,您可以使用

<%= link_to comment.user.username, comment.user %>

一般来说,“Rails Magic”的一部分是,如果正确设置关联,则可以通过点表示法访问相关对象。这意味着,您不需要说comment.user_id,而是直接转到相关的用户对象,例如comment.user.usernamecomment.user.email ...您明白了这一点:)

为此,您应该像这样设置模型:

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

答案 1 :(得分:1)

# Link:
=link_to "User profile", user_path(comment.user)

# Name of the author
=comment.user.username

或者因为MicropostComment

都有user_id
# Link:
=link_to "User profile", (@micropost.user)

# Name of the author
=@micropost.user.username