我试图在单个视图中显示current_user帖子以及他或她关注帖子的用户。无论出于何种原因,我已经创建的帖子都没有加载到视图中。目前数据库中有5个。
饲料控制器
def feed
following_ids = current_user.following_users.map(&:id)
@following_activities = Post.where('user_id in (?)', following_ids.push).order('created_at desc').paginate(page: params[:page])
end
feed.html.erb
<% if @following_activities.any? %>
<% @following_activities.each do |user| %>
<%= link_to(image_tag(user.post.avatar.url(:thumb), style: 'text-decoration: none;', class: 'round-image'), user_path(user)) %>
<%= user.post.username %>
<%= user.post.body %>
<%= image_tag(user.post.photo.url(:medium), style: '') %>
<% end %>
<% else %>
<h1>No new posts</h1>
<% end %>
答案 0 :(得分:0)
似乎你没有向id数组推送任何东西,你应该推current_user.id
。您可以使代码略微清晰,如:
user_ids = current_user.following_users.pluck(:id).push(current_user.id)
@following_activities = Post.where(user_id: user_ids).order('created_at desc').paginate(page: params[:page])
答案 1 :(得分:0)
通过三个步骤,我能够将两者结合起来,现在它正常运作。
def feed
following_ids = current_user.following_users.map(&:id)
following_ids << current_user.id
@following_activities = Post.where(user_id: following_ids).order("created_at desc").paginate(page: params[:page])
end