在我的rails应用程序中,我正在使用gem'meject_as_follower'。我有一个帖子/显示页面,其中我目前显示所有已创建的帖子,但我想更改此内容,以便显示您关注的人发布的帖子!?
我的帖子控制器:
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
我的用户控制器:
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
和我的观点:
<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>
任何人都有任何想法我怎么能这样做?
答案 0 :(得分:1)
据推测,您有一个current_user方法或其他东西来代表登录的用户。
然后在你的帖子控制器
@posts = current_user.follows_by_type('Post').order("created_at DESC")
所以你只得到当前用户关注的帖子。