3

我正在开发一个人们可以喜欢帖子的应用程序。我正在使用 Public Activity gem 和 Acts As Votable。

当我尝试查看页面时出现此错误:

undefined method 'get_upvotes' for PublicActivity::Activity:0x00000101453ad8

get_upvotes 应该与acts_as_votable 一起提供。我究竟做错了什么?

用户模型

class User < ActiveRecord::Base
    acts_as_voter
end

活动模型

class Activity < ActiveRecord::Base
    acts_as_votable
end

查看代码

<% if activity.owner == current_user %>
    <i>This has been liked <strong><%= activity.get_upvotes.size %></strong> times</i>
<% else %>
    <%= link_to like_activity_path(activity),  class: "like", method: :put do %>
      <button type="button" class="btn btn-info" aria-label="Left Align">
        <span class="glyphicon glyphicon-thumbs-up glyphicon-align-center" aria-hidden="true"></span>
        <span class="badge"><%= activity.get_upvotes.size %></span>
      </button>
    <% end %> 
    <a class="btn btn-default btn-xs timeline-button"><i class="fa fa-heart"></i> I Liked This</a>
<% end %>

活动控制器

class ActivitiesController < ApplicationController

    before_action :authenticate_user!, only: [:index, :upvote]

    def index
        @users = current_user.active_friends
        @users.push(current_user)
        case params[:content] when 'posts'
            @activities = PublicActivity::Activity.where(owner_id: @users, trackable_type: "Post").paginate(page: params[:page], per_page: 6).order('created_at DESC')
        else
            @activities = PublicActivity::Activity.where(owner_id: @users).paginate(page: params[:page], per_page: 6).order('created_at DESC')
        end
    end

    def upvote
        @activity = Activity.find(params[:id])
        @activity.upvote_from current_user
        redirect_to activities_path
    end

end

谢谢你尽你所能的帮助。我处于我的 Rails 知识的边缘。:)

4

1 回答 1

0

Activity模型位于PublicActivity模块内部,这意味着这应该可以工作。

class PublicActivity::Activity
  acts_as_votable
end
于 2016-01-05T02:44:40.357 回答