ruby-on-railsrubymodel-view-controlleromniauthpublic-activity

How to show profile pics with activities feed?


How can we include Facebook profile pics in the activities_controller (aka the newsfeed)?

class ActivitiesController < ApplicationController
    def index
      @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.following_ids, owner_type: "User")
    end
end

I get the profile pics from user.rb:

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
    user.provider = auth.provider
    user.image = auth.info.image #specifically this line &
    user.uid = auth.uid          #specifically this line
    user.name = auth.info.name
    user.oauth_token = auth.credentials.token
    user.oauth_expires_at = Time.at(auth.credentials.expires_at)
    user.password = (0...8).map { (65 + rand(26)).chr }.join
    user.email = (0...8).map { (65 + rand(26)).chr }.join+"@mailinator.com"
    user.save!
  end
end

activities index

<% @activities.each do |activity| %>
    <% if @user.uid %>
    <%= image_tag @user.image %>
  <% else %>
    <%= gravatar_for @user %>
  <% end %>
    <%= link_to activity.owner.name, activity.owner if activity.owner %>
    <%= render_activity activity %>
<% end %>

I currently receive this error: undefined method 'uid' for nil:NilClass

Thank you for your expertise!


Solution

  • It seems @user is not assigned in the index function of the ActivitiesController. If there's a relation between activity and user you chould be doing it like this:

    <% @activities.each do |activity| %>
        <% if activity.user.uid %>