Ruby on rails Rails用户有很多帖子,有很多喜欢的帖子
首先,我为英语感到抱歉 所以我已经有了“用户-帖子”一对多的关联,这意味着每个帖子只能有一个作者,现在我想在用户配置文件中添加“收藏夹帖子”按钮,在每个帖子中添加“添加到收藏夹”按钮,所以问题是如何实现这种正确的方式?我应该修改我的用户-帖子关联吗? 或者创建另一个模型?一、 我有点困惑。提前感谢 实际上我想要这个结果: @user.posts#返回此用户创建的所有帖子 @user.favorite_posts#返回此用户添加到收藏夹的帖子 以下是我的用户模型:Ruby on rails Rails用户有很多帖子,有很多喜欢的帖子,ruby-on-rails,associations,Ruby On Rails,Associations,首先,我为英语感到抱歉 所以我已经有了“用户-帖子”一对多的关联,这意味着每个帖子只能有一个作者,现在我想在用户配置文件中添加“收藏夹帖子”按钮,在每个帖子中添加“添加到收藏夹”按钮,所以问题是如何实现这种正确的方式?我应该修改我的用户-帖子关联吗? 或者创建另一个模型?一、 我有点困惑。提前感谢 实际上我想要这个结果: @user.posts#返回此用户创建的所有帖子 @user.favorite_posts#返回此用户添加到收藏夹的帖子 以下是我的用户模型: class User <
class User < ApplicationRecord
mount_uploader :avatar, ImageUploader
validates :username, presence: true, uniqueness: true, length: {in: 3..20}
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :ratings
enum role: [ :user, :admin ]
def calculate_average
ratings.blank? ? 0 : ratings.map(&:value).inject(:+) / ratings.count.to_f
end
end
class用户
后模型:
class Post < ApplicationRecord
mount_uploader :image, ImageUploader
validates :body, presence: true
validates :title, presence: true, length: { maximum: 50}
belongs_to :user
has_many :comments, dependent: :destroy
end
class Post
编辑
好吧,看看我是怎么做到的,它完全按照我想要的方式工作。
这是我的用户模型:
class User < ApplicationRecord
mount_uploader :avatar, ImageUploader
validates :username, presence: true, uniqueness: true, length: {in: 3..20}
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :ratings
has_many :favorites, dependent: :destroy
has_many :favorite_posts, through: :favorites, source: "post"
enum role: [ :user, :admin ]
def calculate_average
ratings.blank? ? 0 : ratings.map(&:value).inject(:+) / ratings.count.to_f
end
end
class用户
您需要收藏夹帖子的关系,首先运行此命令创建一个表收藏夹帖子
rails g model FavoritePost user:references post:references
然后
然后将这些添加到模型中,如下所示:
#=> model/user.rb
class User < ApplicationRecord
has_many :favorite_posts, dependent: :destroy # or you can use only this line except second if you will face any problem
has_many :posts, through: :favorite_posts
end
#=> model/post.rb
class Post < ApplicationRecord
has_many :favorite_posts, dependent: :destroy
has_many :users, through: :favorite_posts
end
#=> model/favorite_post.rb
class FavoritePost < ApplicationRecord
belongs_to :user
belongs_to :post
end
# For creating favorite
<%= link_to "Favorite", favorites_path(user: current_user, post: post.id), class: 'btn bf-save-btn', method: :post, data: {disable_with: "Saving..."}, title: "Add to favorite" %>
# For deleting favorite list
<%= link_to "Unfavorite", favorite_path(post.id), class: 'btn af-save-btn', method: :delete, data: {disable_with: "Removing...."}, title: "Remove from favorite" %>
然后您的路由文件:
resources :favorites
使用耙路由的新路由示例:
favorites GET /favorites(.:format) favorites#index
POST /favorites(.:format) favorites#create
new_favorite GET /favorites/new(.:format) favorites#new
edit_favorite GET /favorites/:id/edit(.:format) favorites#edit
favorite GET /favorites/:id(.:format) favorites#show
PATCH /favorites/:id(.:format) favorites#update
PUT /favorites/:id(.:format) favorites#update
DELETE /favorites/:id(.:format) favorites#destroy
在视图文件中添加如下内容:
#=> model/user.rb
class User < ApplicationRecord
has_many :favorite_posts, dependent: :destroy # or you can use only this line except second if you will face any problem
has_many :posts, through: :favorite_posts
end
#=> model/post.rb
class Post < ApplicationRecord
has_many :favorite_posts, dependent: :destroy
has_many :users, through: :favorite_posts
end
#=> model/favorite_post.rb
class FavoritePost < ApplicationRecord
belongs_to :user
belongs_to :post
end
# For creating favorite
<%= link_to "Favorite", favorites_path(user: current_user, post: post.id), class: 'btn bf-save-btn', method: :post, data: {disable_with: "Saving..."}, title: "Add to favorite" %>
# For deleting favorite list
<%= link_to "Unfavorite", favorite_path(post.id), class: 'btn af-save-btn', method: :delete, data: {disable_with: "Removing...."}, title: "Remove from favorite" %>
这就是最喜欢/最不喜欢的功能。现在,您需要为何时显示Favorite
和何时显示Unfavorite
创建一些逻辑
因为这种需求有很多种方式,首先你需要理解这一点,然后你可以随心所欲
另外,为了在不重新加载页面的情况下实现这一点,您可以尝试一些Ajax
更新
class User < ApplicationRecord
mount_uploader :avatar, ImageUploader
validates :username, presence: true, uniqueness: true, length: {in: 3..20}
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :ratings
# Newly added
has_many :favorite_posts, dependent: :destroy # or you can use only this line except second if you will face any problem
has_many :posts, through: :favorite_posts
enum role: [ :user, :admin ]
def calculate_average
ratings.blank? ? 0 : ratings.map(&:value).inject(:+) / ratings.count.to_f
end
end
class用户
希望能有所帮助。为“UserFavoritePost”存储的帖子id和用户id创建一个新模型。并为收藏夹帖子创建自定义关联
class UserFavoritePost < ApplicationRecord
belongs_to :post
belongs_to :user
end
class User < ApplicationRecord
has_many :posts, dependent: :destroy
has_many :user_favorite_posts
has_many :favorite_posts, throught: :user_favorite_posts, class: 'Post'
end
class-UserFavoritePost
傻瓜开发人员的回答不提供对喜爱文章的直接访问,因此索引视图需要一个循环。普拉桑纳的方法解决了这个问题,但他的答案被不公平地指责为不完整和剽窃:-)。下面是完整的方法:
您需要一个多对多关系,这是事实,因此您需要一个连接模型和表。但这种模式是辅助的。没有重要的逻辑应该存在,我认为它不值得一个控制器或视图
class User < ApplicationRecord
has_many :posts, dependent: :destroy # Posts created by this user
has_many :favs, dependent: :destroy
has_many :fav_posts, through: :favs # Favorite posts for this user
end
class Post < ApplicationRecord
belongs_to :user
has_many :favs, dependent: :destroy
has_many :fav_users, through: :favs # Users who have this post as favorite
end
class Fav < ApplicationRecord
belongs_to :user
belongs_to :post
end
他认为:
<h1><% current_user.name %></h1>
<h2>Your posts</h2>
<%= render @posts %>
<h2>Your favorite posts from other users</h2>
<%= render @fav_posts %>
<%= link_to "Favorite", fav_post_path(id: post.id) %>
<%= link_to "Unfavorite", unfav_post_path(id: post.id) %>
我是编程新手,我读过rails指南中关于关联的内容,但我不知道如何在我的案例中使用它,我的意思是每个用户都有许多由他创建的帖子,他也有许多他标记为最喜欢的帖子,但这些最喜欢的帖子不一定属于这个用户。我在上面写了我想要的:)谢谢,我会尝试的好的,但现在我无法在我的帖子视图中键入@post.user.username来显示帖子作者,对吗?也许可以将post
模型的has-many:users
关联重命名为类似has-many:favoriting\u-users
(或其他?)@fool-dev,让Kizlo更清楚一点?谢谢你的解释,但我仍然不明白我现在应该如何在我的帖子视图中显示作者用户名,因为我现在拥有的是类似的,但在你的代码帖子中有很多users@Kizlokizlo,看你的模型和以前一样,新增加了两行,比如有很多:喜欢的帖子,dependent::destroy有很多:帖子,通过::favorite_posts
您不需要编辑任何内容,只需添加这两行。这是不完整的,不是吗?另外,@fool-dev已经提交的答案没有包括哪些内容?@fool-dev的答案返回user.favorite_posts不会返回实际的posts记录。返回post_id和user_id.Um,是的
<h1><% current_user.name %></h1>
<h2>Your posts</h2>
<%= render @posts %>
<h2>Your favorite posts from other users</h2>
<%= render @fav_posts %>
def fav
current_user.fav_posts << Post.find(params[:id])
end
def unfav
current_user.favs_posts.destroy(Post.find(params[:id]))
end
<%= link_to "Favorite", fav_post_path(id: post.id) %>
<%= link_to "Unfavorite", unfav_post_path(id: post.id) %>
post '/posts/:id/fav', to: 'posts#fav', as: 'fav_post'
post '/posts/:id/unfav', to: 'posts#unfav', as: 'unfav_post'