用户镜头 - 如何仅显示您关注的用户(不包括我自己的用户)的镜头



如何仅显示您关注的用户(不包括我自己的用户(的镜头? 我认为问题出在提要上,但我对 Rails 很陌生,不知道该怎么做。谢谢开发人员!

从显示页面拍摄

[enter image description here][1]
my user.controller
class User < ApplicationRecord
friendships, class_name: "Friendship", foreign_key: "follower_id", dependent: :destroy
has_many :passive_friendships, class_name: "Friendship", foreign_key: "followed_id", dependent: :destroy
has_many :followings, through: :active_friendships, source: :followed
has_many :followers, through: :passive_friendships, source: :follower
#follow another user
def follow(other_user)
active_friendships.create(followed_id: other_user.id)
end
#Unfollow a other_user
def unfollow(other_user)
active_friendships.find_by(followed_id: other_user.id).destroy
end
#Is following a other_user?
def following?(other_user)
following_ids.include?(other_user.id)
end
def feed
following_ids = "SELECT followed_id FROM Friendships WHERE follower_id = :user_id"
Shot.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id)
end
my show.html.erb
<div class="shots user">
<% @user.feed.each do|shot|%>
<section class="section">
<div class="shot-grid-item">
<div class="shot-wrapper">
<%= link_to shot, class: "shot" do %>
<% if shot.user_shot_url.present? %>
<%= cl_image_tag(shot.user_shot_url) %>
<% else %>
<label><%= shot.description %></label>
<div class="shot-data">
<h3 class="shot-title"><%= shot.title %></h3>
<div class="shot-description"><%= truncate(shot.description, length: 60) %></div>
<div class="shot-time">
<%= time_ago_in_words(shot.created_at) %>
</div>
</div>
<% end %>
<% end%>
</div>

你可以做类似的事情

<%= current_user.followings.shots.each do |shot| %>
<% end %>

更好 - 您可以在控制器中声明一个变量,该变量从视图中提取此逻辑:

@following_shots = current_user.followings.shots

然后在视图中

<%= @following_shots.each do |shot| %>
<% end %>

最新更新