我遵循迈克尔·哈特尔的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
关系控制器
类RelationshipsController&lt; ApplicationController中
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
我已经坚持这个问题太久了。任何帮助将不胜感激。如果您需要更多信息,请随时提出问题。
如何编辑“活动”控制器以显示跟随用户的活动?
**编辑:它有效!!
答案 0 :(得分:0)
免责声明:我对public_activity gem一无所知,但这里有文档建议您需要做的事情:
过滤您显示的活动:PublicActivity::Activity.where(owner_type: "User", owner_id: current_user.followed_users.map {|u| u.id}).all
(这基于this sample code.)