3

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!

AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80
  • 1
    The error indicates the `@user` doesn't exist. You need to load `@user` in your controller. Like `@user = current_user` if you're using devise – kartikluke Apr 16 '15 at 20:37
  • @kartikluke that's close but `@user` needs to be the user of who made the post not the `current_user` looking at the post. Thanks tho at least that got a profile picture showing - let's just get the right one =) – AnthonyGalli.com Apr 17 '15 at 00:29
  • I was giving an example from experience. Help on the error type so you could debug it better. – kartikluke Apr 17 '15 at 15:28

1 Answers1

3

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 %>
sourcx
  • 944
  • 7
  • 22