0

我正在为一个项目使用acts_as_votable gem,该项目将允许用户“喜欢”他们最喜欢的课程和他们最喜欢的指南。然后收藏的指南将显示在一个页面上,而收藏的课程将显示在另一个页面上。我在我的控制器中检索模型特定结果时遇到了问题,下面是有效但没有作用于特定控制器的代码。

class FavoritesController < ApplicationController
 def guides

 end

 def courses
  user = current_user
  @courses = user.find_up_voted_items
 end
end

这是我开始工作的唯一代码,我意识到控制器中目前没有任何东西可以将结果缩小到特定模型,但我无法得到任何我试图工作的东西。

4

1 回答 1

0

From the acts_as_votable docs:

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

So in your case I would use:

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
于 2018-02-10T06:03:57.750 回答