如何显示Rails关注用户的头像
How to show the gravatar of the user who is following in Rails
全部,我目前正在学习 Rails 并着手进行一个项目,我 运行 陷入 issue.I 我无法显示 [=32] 的用户的图像=] 属于。
当我转到主页时,我应该会看到您正在关注的用户的 post...不幸的是,我不确定如何显示 [=] 用户的图像32=] 属于....我可以展示他们的 post 但不确定我如何展示他们的形象。我必须说我正在使用回形针 gem.
用户模型
class User < ActiveRecord::Base
# 包括默认设计模块。其他可用的是:
# :confirmable, :lockable, :timeoutable 和 :omniauthable
设计 :database_authenticatable, :registerable,
:可恢复,:可记忆,:可跟踪,:可验证
has_attached_file :avatar, :styles => { :medium => "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
我可以获得当前用户的图像,但是如果我登录我的帐户,并且我正在关注某人,我想看到他们的图像而不是我的图像,因为他们关联 post..
<%=image_tag(current_user.avatar.url, class: "img-circle img-responsive img-raised", :size => "100x100") %>
你的 post 必须在数据库中有 belongs_to :user
和 user_id
,然后你可以这样做:
@posts.each do |post|
<%=image_tag(post.user.avatar.url, class: "img-circle img-responsive img-raised", :size => "100x100") %>
end
但我看到你有 has_many :avatar
可能是你在此处编码 post 的错误,如果不是你必须先 select 你想要 avatar_url使用。
全部,我目前正在学习 Rails 并着手进行一个项目,我 运行 陷入 issue.I 我无法显示 [=32] 的用户的图像=] 属于。
当我转到主页时,我应该会看到您正在关注的用户的 post...不幸的是,我不确定如何显示 [=] 用户的图像32=] 属于....我可以展示他们的 post 但不确定我如何展示他们的形象。我必须说我正在使用回形针 gem.
用户模型
class User < ActiveRecord::Base
# 包括默认设计模块。其他可用的是: # :confirmable, :lockable, :timeoutable 和 :omniauthable 设计 :database_authenticatable, :registerable, :可恢复,:可记忆,:可跟踪,:可验证 has_attached_file :avatar, :styles => { :medium => "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
我可以获得当前用户的图像,但是如果我登录我的帐户,并且我正在关注某人,我想看到他们的图像而不是我的图像,因为他们关联 post..
<%=image_tag(current_user.avatar.url, class: "img-circle img-responsive img-raised", :size => "100x100") %>
你的 post 必须在数据库中有 belongs_to :user
和 user_id
,然后你可以这样做:
@posts.each do |post|
<%=image_tag(post.user.avatar.url, class: "img-circle img-responsive img-raised", :size => "100x100") %>
end
但我看到你有 has_many :avatar
可能是你在此处编码 post 的错误,如果不是你必须先 select 你想要 avatar_url使用。