如何显示活动Feed的个人资料照片?

时间:2015-04-16 20:29:10

标签: ruby-on-rails ruby model-view-controller omniauth public-activity

我们如何在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

感谢您的专业知识!

1 个答案:

答案 0 :(得分:3)

看来@user未在ActivitiesController的索引函数中分配。如果活动与用户之间存在关系,您可以这样做:

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