使用 acts as followable 只显示来自用户关注的人的推文
using acts as followable to only display tweets from people that a user follows
目前在我的 Twitter 克隆上,每个用户的每条推文都显示在索引页面上,但我只想显示用户关注的人的推文,用户可以在另一个用户显示页面上执行此操作。我正在尝试使用 acts as followable 但我不确定如何去做,主要是 tweets 索引控制器和用户显示视图。到目前为止我所拥有的如下,其中大部分是我在网上找到的,谢谢。如果需要更多信息,请告诉我。
<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
忘记了?在追随者部分之后
目前在我的 Twitter 克隆上,每个用户的每条推文都显示在索引页面上,但我只想显示用户关注的人的推文,用户可以在另一个用户显示页面上执行此操作。我正在尝试使用 acts as followable 但我不确定如何去做,主要是 tweets 索引控制器和用户显示视图。到目前为止我所拥有的如下,其中大部分是我在网上找到的,谢谢。如果需要更多信息,请告诉我。
<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
忘记了?在追随者部分之后