ruby on rails - How to get a timeline of posts of only people someone follows -
i'm trying instagram can see images of people follow. user can follow user , create new posts.
this have following , unfollowing in users controller.
def following @user = user.find(params[:id]) current_user.mark_as_following @user respond_to |format| format.html {redirect_to @user} format.js end end def unfollow @user = user.find(params[:id]) @user.unmark :following, :by => current_user respond_to |format| format.html {redirect_to @user} format.js end end
here my posts controller
class postscontroller < applicationcontroller load_and_authorize_resource def show @post = post.find(params[:id]) end def new @post = post.new end def create @post.user_id = current_user.id if @post.save redirect_to @post else render :new end end def edit @post = post.find(params[:id]) end def update @post = post.find(params[:id]) if @post.update_attributes(update_params) redirect_to @post else render :edit end end private def update_params params.require(:post).permit(:caption, :image) end def create_params params.require(:post).permit(:caption, :user_id, :image) end end
you can create controller this
class followingpostscontroller < applicationcontroller def index @posts = current_user.following_posts end end
and in user model
class user < activerecord::base def following_posts #assuming following_users returns list of following users self.following_users.map{ |user| user.posts }.flatten(1) end end
or can list of posts :
post.where(user_id: self.following_users.ids)
Comments
Post a Comment