【发布时间】:2015-04-29 11:09:11
【问题描述】:
我正在开发一个人们可以点赞帖子的应用。我正在使用 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 知识的边缘。 :)
【问题讨论】:
-
尝试将 Public Activity gem 提取到您的 vendor/gems 文件夹并转到 /vendor/gems/public_activity-1.4.2/lib/public_activity/orm/active_record /activity.rb 并在那里申请
acts_as_votable并检查它是否有效。 -
使用 activity.likes.size 或更多更好地显示使用
-
更改它会返回此错误:未定义的方法 `likes'
标签: ruby-on-rails public-activity