2017-01-22 29 views
0

所有,我目前正在学习Rails并继续进行一个项目,并且我遇到了一个问题。我无法显示该帖子所属用户的图像至。如何显示在Rails中跟随的用户的gravatar

当我进入主页时,我应该看到你正在关注的用户的帖子......并且不幸的是我不确定我如何显示帖子所属用户的图像....我可以显示他们的帖子,但不知道我如何显示他们的形象。我必须说我正在使用回形针宝石。

用户模型

class User < ActiveRecord::Base 

#包括默认设计模块。可用其他是: #:可证实,:可锁定,:timeoutable和:omniauthable 色器件:database_authenticatable,:登记的, :采,:rememberable,:可追踪的,:可验证 has_attached_file:头像,:风格=> {:中等= >“300x300>”,:thumb =>“100x100#”},::default_url =>“/images/:style/missing.png” validates_attachment_content_type:avatar,:content_type => /\Aimage/.*\Z/

 has_many :followeds, through: :relationships 
     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" 
    has_many :reverse_relationships, foreign_key: "followed_id", 
           class_name: "Relationship", 
           dependent: :destroy 
    has_many :followers, through: :reverse_relationships, source: :follower 


    has_many:avatar, dependent: :destroy 
    has_many :posts, dependent: :destroy # remove a user's posts if his account is deleted. 
    has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy 
    has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy 

    has_many :following, through: :active_relationships, source: :followed 
    has_many :followers, through: :passive_relationships, source: :follower 




    def avatar_url 
    avatar.url(:medium) 
    end 

    # helper methods 

    # follow another user 
    def follow(other) 
     active_relationships.create(followed_id: other.id) 
    end 

    # unfollow a user 
    def unfollow(other) 
     active_relationships.find_by(followed_id: other.id).destroy 
    end 

    # is following a user? 
    def following?(other) 
     following.include?(other) 

    end 
    end 

我可以获取当前用户的形象,但如果我登录到我的账户,我关注的人,我希望看到自己的形象而不是我为他们的相关帖子..

   <%=image_tag(current_user.avatar.url, class: "img-circle img-responsive img-raised", :size => "100x100") %> 

回答

0

您的帖子已在数据库belongs_to :useruser_id,那么你可以做这样的:

@posts.each do |post| 
    <%=image_tag(post.user.avatar.url, class: "img-circle img-responsive img-raised", :size => "100x100") %> 
end 

但我看你有没有has_many :avatar它可以在你的代码贴在这里的错误,如果不是你必须先选择你想使用的avatar_url。

+0

This Works !!非常感谢你的回应! –