Search code examples
ruby-on-railsrubyacts-as-follower

using acts as followable to only display tweets from people that a user follows


currently on my twitter clone, every tweet from every user is displayed on the index page, but i want to only display the tweets from the people that a user follows, which a user can do on another users show page. im trying to use acts as followable but im not sure how to go about it, mainly the tweets index controller and the user show view. what i have so far is below, most of which i found online, thanks. let me know if any more info needed.

<h1 class="profile-title text-primary"><%= @user.username  %> </h1>
<div class="avatar-show">
  <%= cl_image_tag @user.photo, height: 200, width: 200, crop: :fill %>
</div>

<% if current_user.following(@user) %>
  <%= button_to "Following", {action: "unfollow", id: @user.id}, method: "post", class: "btn btn-secondary btn_unfollow", remote: true %>
<% elsif current_user != @user %>
  <%= button_to "Follow", {action: "follow", id: @user.id}, method: "post", class: "btn btn-primary btn_follow", remote: true %>
<% end %>

<div id="tweets">
  <% @tweets.each do |tweet|  %>
  <div id="tweet-<%= tweet.id %>" class="card cardspace">
    <p class="photo"><%= cl_image_tag tweet.user.photo, height: 50, width: 50, crop: :fill %></p>
    <h4 class = "username"><%= link_to tweet.user.username, user_path(tweet.user) %></h4>
    <p class="tweet-content"><%= tweet.content %></p>
    <p class="date"><%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %></p>
    <p class = "icons">Upvotes <%= tweet.get_upvotes.size %> | Downvotes <%= tweet.get_downvotes.size %></p>
 <% end %>
</div>

class UsersController < ApplicationController


  def show

    @user = User.find(params[:id])
    @tweets = @user.tweets.order('created_at DESC')
    authorize @user
  end

  def follow
     @user = User.find(params[:id])
      current_user.follow(@user)
      redirect to user_path(@user)
    # @current_user.follow(@user)
    # @follow = Follow.find_by(follower: @current_user, followable: @user)
    # respond_to :js
  end

  def unfollow
    @user = User.find(params[:id])
    current_user.stop_following(@user)
    redirect_to user_path(@user)
    # @current_user.stop_following(@user)
    # respond_to :js
  end
end

class TweetsController < ApplicationController
  # before_action :authenticate_user!, :except => [:index]
  require "open-uri"
   def index
    # @tweets = Tweet.all.order("created_at DESC")
    # @tweets = policy_scope(Tweet).paginate(page: params[:page], per_page: 7).order('created_at DESC')

    # authorize @tweets
    if params[:query].present?
      @tweets = policy_scope(Tweet).global_search(params[:query]).paginate(page: params[:page], per_page: 5).order('created_at DESC')
    else
    @tweets = policy_scope(Tweet).paginate(page: params[:page], per_page: 5).order('created_at DESC')

    end
    @tweet = Tweet.new
    @user = current_user
    url = 'https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=**************************'
    article_serialized = open(url).read
    @articles = JSON.parse(article_serialized)
    @users = User.all.limit(5)
  end


Solution

  • forgot the ? after follower part