2016-03-02 50 views
0

我想顯示current_user帖子和用戶,他或她是在單個視圖後的帖子。無論出於何種原因,我已經創建的帖子都沒有加載到視圖中。數據庫中目前有5個。結合當前的用戶帖子和追隨者的帖子瓦特/ Acts_as_Follower Rails 4

供應控制

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

看來你是推沒什麼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]) 
+0

它現在說#undefined方法'頭像'#當我使用您的查詢方法。有什麼線索爲什麼? –

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