私はMichaelHartlの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<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
私はこの問題に長い間立ち往生してきました。どんな助けでも大歓迎です。さらに詳しい情報が必要な場合は、お気軽にご質問ください。
フォローしているユーザーのアクティビティを表示するようにアクティビティコントローラーを編集するにはどうすればよいですか?
**編集:それは動作します!!