0

在我的 Rails 应用程序中,我使用的是 gem 'acts_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>

有人知道我该怎么做吗?

4

1 回答 1

1

大概你有一个 current_user 方法或其他东西来代表登录的用户。

然后在您的帖子控制器中

@posts = current_user.follows_by_type('Post').order("created_at DESC")

因此,您只能获得当前用户关注的帖子。

于 2013-03-23T13:26:46.193 回答