gpt4 book ai didi

ruby-on-rails - 如何在事件提要中显示个人资料照片?

转载 作者:数据小太阳 更新时间:2023-10-29 07:59:28 26 4
gpt4 key购买 nike

我们如何在 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。如果事件和用户之间存在关系,您应该这样做:

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

关于ruby-on-rails - 如何在事件提要中显示个人资料照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29685339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com