我正在开发一个人们可以喜欢帖子的应用。我使用了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知识的边缘。 :)
答案 0 :(得分:0)
Activity
模型位于PublicActivity
模块中,这意味着这应该有效。
class PublicActivity::Activity
acts_as_votable
end