如何在活动提要中显示个人资料照片?

How to show profile pics with activities feed?

我们如何在 activities_controller(又名新闻源)中包含 Facebook 个人资料照片?

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

我从 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.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 %>

我目前收到此错误:undefined method 'uid' for nil:NilClass

感谢您的专业知识!

在ActivitiesController 的索引函数中似乎没有分配@user。如果 activity 和用户之间存在关系,你应该这样做:

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