0

我按照 Michael Hartl 的 Rails 教程实现了一个类似于 Twitter (railstutorial.org) 的追随者系统。目前,我想使用此关注系统仅显示当前登录用户所关注的用户的活动。我可以在 /activities 页面上显示系统中的所有活动,没有问题。工作的活动控制器代码如下:

class ActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.all
  end
end

活动控制器的索引页面列出了所有用户发表的所有帖子。但是,我想编辑控制器代码以仅显示关注用户的活动。我怎样才能做到这一点?我所有的尝试都没有导致方法错误。以下是相关项目:

用户型号:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :title, :location, :aboutme, :works_attributes
  # attr_accessible :title, :body

  validates_presence_of :first_name, :last_name
  validates_uniqueness_of :first_name, :last_name, :email, :case_sensitive => false

  has_many :works, dependent: :destroy
  accepts_nested_attributes_for :works

  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_users, through: :relationships, source: :followed

  has_many :reverse_relationships, foreign_key: "followed_id",
                                   class_name:  "Relationship",
                                   dependent:   :destroy
  has_many :followers, through: :reverse_relationships, source: :follower

  has_many :posts, dependent: :destroy

   def full_name
    [first_name, last_name].join(" ")
  end

  def following?(other_user)
    relationships.find_by_followed_id(other_user.id)
  end

  def follow!(other_user)
    relationships.create!(followed_id: other_user.id)
  end

  def unfollow!(other_user)
    relationships.find_by_followed_id(other_user.id).destroy
  end
end

关系模型:

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id, :follower_id

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, presence: true
  validates :followed_id, presence: true
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])
    @posts = @user.posts.all
    @works = @user.works.all
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end
   def following
    @title = "Following"
    @user = User.find(params[:id])
    @users = @user.followed_users.all
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user = User.find(params[:id])
    @users = @user.followers.all
    render 'show_follow'
  end

  def posts
    @user = User.find(params[:id])
    @posts = @user.posts.all
    render 'show_post'
  end
end

关系控制器

类关系控制器 < 应用控制器

  def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
   respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end
end

我已经被这个问题困了太久了。任何帮助将不胜感激。如果您需要更多信息,请随时提出问题。

如何编辑活动控制器以显示关注用户的活动?

**编辑:它有效!

4

1 回答 1

0

免责声明:我对 public_activity gem 一无所知,但这是文档建议您需要做的事情:

  1. 为您的活动设置“所有者”属性。

  2. 过滤您显示的活动:(PublicActivity::Activity.where(owner_type: "User", owner_id: current_user.followed_users.map {|u| u.id}).all这是基于此示例代码。

于 2013-03-24T21:10:41.603 回答