我创建了两组不同的编码,假设允许用户关注(书签)另一个用户。例如,用户 A 关注用户 B、C 和 D。用户 A 转到他们的收藏夹页面,其中列出了他们关注的所有用户(B、C 和 D)。这将是一种方式。我创建的代码没有执行关注用户的操作,所以我决定遵循 railstutorial Twitter 关注概念。我得到一个“未初始化的常量RelationshipsController”。所以路线有问题。
有人可以看看,也许指出什么是错的?
顺便说一句,这感觉就像为如此简单的事情编写了很多代码……即使我自己在没有遵循教程的情况下编写代码也是如此。我只是希望用户能够为另一个用户页面网址添加书签,将其保存到某个收藏夹页面(本质上是一个书签页面),并为他们提供删除该书签的选项。我认为这甚至不需要数据库。
路线:
resources :sessions, only: [:new, :create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :users do
get 'settings', on: :member
end
用户模型:
has_many :notifications
has_many :users, dependent: :destroy
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
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