也一样
我有一个基本的博客应用程序,其中for auth for Auth,并试图在不使用gem和has_many关联的情况下将书签函数相互分布。我该怎么做?
我的User
型号:
class User < ActiveRecord::Base
belongs_to :user
has_many :posts
has_many :posts, :through => :bookmarks
end
我的Post
型号:
class Post < ActiveRecord::Base
has_many :bookmarked_posts, through: :bookmarks, source: :post
end
我的Boomark
型号:
class Bookmark < ActiveRecord::Base
belongs_to :job
belongs_to :user
end
1,用户模型中有2次has_many :posts
,这是不正确的,首先更改为 has_many :own_posts, foreign_key: :user_id, class_name: "Post"
-
创建用于在书签和路由中添加帖子的新控制器。RB
-
添加方法
def add
current_user.posts << Post.find(params[:post_id])
end
实际上可以在scontroller中添加,但是无论如何都必须在routes.rb中添加新操作。从书签中删除帖子