0
votes

All, i am currently learning Rails and proceeding with a project and i am running into an issue.I am unable to show the image of the user who the post belongs to.

When i go to the home page, i should see the post of the users who you are following ... and unfortunately i am not sure how i can show the image of user who the post belongs to.... i can show their post but not sure how i show their image. I must say i am using paperclip gem.

user model

class User < ActiveRecord::Base

# Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable 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

i can get the current user image, but if i log into my account, and i am following someone, i want to see their image not mine for their associated post..

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

1 Answers

0
votes

Your post has to have belongs_to :user and user_id in database, then you can do it like this:

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

But i see you have has_many :avatar it could be mistake in you code posted here, if not you have to first select which avatar_url you want to use.