如何获得特定型号的 acts_as_votable 结果?
How do I get acts_as_votable results for a specific model?
我正在与 acts_as_votable gem 合作进行一个项目,该项目将允许用户 'like' 他们最喜欢的课程和他们最喜欢的指南。然后,收藏的指南将显示在一页上,而收藏的课程将显示在另一页上。我在我的控制器中检索特定于模型的结果时遇到问题,下面是有效但未限定特定控制器范围的代码。
class FavoritesController < ApplicationController
def guides
end
def courses
user = current_user
@courses = user.find_up_voted_items
end
end
这是我唯一可以工作的代码,我意识到目前控制器中没有任何东西可以将结果缩小到特定模型,但我无法得到我尝试工作的任何东西。
来自 acts_as_votable 文档:
Members of an individual model that a user has voted for can also be
displayed. The result is an ActiveRecord Relation.
@user.get_voted Comment
@user.get_up_voted Comment
@user.get_down_voted Comment
https://github.com/ryanto/acts_as_votable
所以在你的情况下我会使用:
class FavoritesController < ApplicationController
before_action :get_user, only: %i[guides courses]
def guides
@guides = @user.get_up_voted Guide
end
def courses
@courses = @user.get_up_voted Course
end
private
def get_user
@user = current_user
end
end
我正在与 acts_as_votable gem 合作进行一个项目,该项目将允许用户 'like' 他们最喜欢的课程和他们最喜欢的指南。然后,收藏的指南将显示在一页上,而收藏的课程将显示在另一页上。我在我的控制器中检索特定于模型的结果时遇到问题,下面是有效但未限定特定控制器范围的代码。
class FavoritesController < ApplicationController
def guides
end
def courses
user = current_user
@courses = user.find_up_voted_items
end
end
这是我唯一可以工作的代码,我意识到目前控制器中没有任何东西可以将结果缩小到特定模型,但我无法得到我尝试工作的任何东西。
来自 acts_as_votable 文档:
Members of an individual model that a user has voted for can also be displayed. The result is an ActiveRecord Relation.
@user.get_voted Comment @user.get_up_voted Comment @user.get_down_voted Comment
https://github.com/ryanto/acts_as_votable
所以在你的情况下我会使用:
class FavoritesController < ApplicationController
before_action :get_user, only: %i[guides courses]
def guides
@guides = @user.get_up_voted Guide
end
def courses
@courses = @user.get_up_voted Course
end
private
def get_user
@user = current_user
end
end