Reputation: 239
In my rails app I am using the gem 'acts_as_follower'. I have a posts/show page in which I currently show ALL posts ever created, although I want to change this so that it displays posts from people you follow!?
My Posts Controller :
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.all(:order => "created_at DESC")
@post = Post.new
@users = User.all(:order => "created_at DESC")
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
# GET /posts/1
# GET /posts/1.json
def show
redirect_to posts_path
end
# GET /posts/new
# GET /posts/new.json
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
@post = current_user.posts.build(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.json
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
And my Users Controller :
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
def follow
@user = User.find(params[:id])
current_user.follow(@user)
redirect_to root_path
end
def unfollow
@user = User.find(params[:id])
current_user.stop_following(@user)
redirect_to root_path
end
end
And My View :
<div class="dash-well">
<div class="gravatar-dashboard">
<%= image_tag avatar_url(@user), :class => 'gravatar-pos-fix gr-dash-mar-top' %>
<h1 class="nuvo wtxt"><%= @user.username.capitalize %></h1>
<h3 class="nuvo wtxt"><%= @user.motto %></h3>
<div class="follower-count nuvo"><%= @user.followers_count %> Followers</div>
<%= link_to "Follow", follow_user_path(@user) %>
</div>
</div>
<div class="dash-well-status">
<% @post.each do |post| %>
<div class="dash-post">
<div class="dash-tstamp">
<strong>Posted <%= time_ago_in_words(post.created_at) %> ago</strong>
</div>
<div class="dash-status"><%= post.status %></div>
</div>
<% end %>
</div>
Anyone have any ideas how I could do that?
Upvotes: 0
Views: 785
Reputation: 3499
Presumably you have a current_user method or something else to represent the user who is logged in.
Then in your posts controller
@posts = current_user.follows_by_type('Post').order("created_at DESC")
So you only get the posts the current user is following.
Upvotes: 1