используя действия, как следует для показа только твиты от людей, за которыми следует пользователь - PullRequest
0 голосов
/ 19 октября 2018

в настоящее время в моем клоне в твиттере каждый твит от каждого пользователя отображается на странице индекса, но я хочу отображать твиты только тех людей, за которыми следит пользователь, что пользователь может сделать на странице других пользователей.Я пытаюсь использовать действия, как следует, но я не уверен, как это сделать, в основном контроллер индекса твитов и пользовательский вид представления.то, что у меня есть, ниже, большинство из которых я нашел в Интернете, спасибо.дайте мне знать, если потребуется дополнительная информация.

<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

1 Ответ

0 голосов
/ 21 октября 2018

забыли?после ведомой части

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...