繁体   English   中英

Ruby on Rails和Twitter Gem-每当有人登录时,即使在其他帐户中,我仍会不断吸引我的第一批用户

[英]Ruby on Rails and Twitter Gem - I keep getting my first users followers everytime someone signs in, even in different accounts

我有一个与omniauth gem连接的应用程序,twitter吸引了所有用户关注者,并将其显示在网页上。 但是,当另一个用户登录时,情况有些变化,他们的当前关注者不会出现,只有第一个登录用户的几个关注者。

每当有人登录时,即使使用不同的帐户,我都会得到我的第一批用户关注者。

每当有人登录并对其帐户进行oAuth认证时,我都会获得正确的用户信息,但是当我查看关注者时,它只会显示与第一个用户相同的信息。

我的控制器或索引有什么问题,或者该怎么办?

这是我的页面控制器

class PagesController < ApplicationController
  def index
    ufs = UserFollower.where(user_id: session[:user_id]) #user_id, follower_id
    follower_ids = ufs.map{|u| u.follower_id} # follower_id
    @followers = Follower.where(id: follower_ids)


    if @followers.blank?
      twitter_response = TwitterAPI.new
      @followers = twitter_response.client.followers
      @followers.each do |follower|
        f = Follower.find_or_create_by(name: follower.name, user_id: follower.id)
        UserFollower.create(user_id: session[:user_id], follower_id: f.id)
      end
    end
  end
end

我的index.html.erb

<% if current_user %>
  <div class="page-header text-center">
    <h2>Your Twitter Information & Followers</h2>
  </div>
  <div class="panel panel-default">
    <div class="panel-body card border-primary cardlist text-center">
      <ul>
        <span class="profile"><%= image_tag current_user.image_url, alt: current_user.name %></span><br>
        <span class="profile"><%= current_user.name %></span><br>
        <span><strong>URL:</strong> <%= current_user.url %></span><br>
        <span><strong>Location:</strong> <%= current_user.location %></span><br>
        <span> <strong>Provider:</strong> <%= current_user.provider %></span><br>
        <span><strong>uID:</strong> <%= current_user.uid %></span><br>
      </ul>
    </div>


<h2 class="followerlist text-center"> Followers</h2>
<div class="card-columns">
<ul class="fa-ul">
  <% @followers.each do |follower| %>
    <li class="card border-primary cardlist w-60 cardclick"><i class="fa-li fab fa-twitter-square"></i><%= follower.name %></li>
  <% end %>
</ul>
</div>
</div>

  </div>
<% else %>
  <div class="jumbotron text-center">
  <h1>Welcome!</h1>
  <p>Authenticate via twitter to get started.</p>
  <button type="button" class="btn btn-outline-primary btn-lg"><%= link_to 'Sign in with Twitter', '/auth/twitter' %></button>
</div>
<% end %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM