我遵循Michael Hartl的Rails Tutorial來實現類似於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
關係控制器
階級關係hipsController < 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
我一直在這個問題上停留太久。任何幫助將不勝感激。如果您需要更多信息,請隨時提問。
如何編輯活動控制器以便顯示跟隨用戶的活動?
**編輯:它的作品!
你可以發佈你的代碼活動模型?用戶和活動如何相互作用? – drewinglis 2013-03-24 20:46:34
感謝您的回覆!我沒有一個活動模型。我完全忘了提及我使用public_activity gem來處理活動(https://github.com/pokonski/public_activity)。 – winston 2013-03-24 20:55:29