我正在开发一款人们可以为帖子点赞的应用。我正在使用 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 %>
事件 Controller
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 知识的边缘。 :)
最佳答案
Activity
模型位于 PublicActivity
模块中,这意味着它应该可以工作。
class PublicActivity::Activity
acts_as_votable
end
关于ruby-on-rails - 在公共(public)事件中担任投票人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29941943/