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知識的邊緣。 :)
嘗試提取**公共活動**寶石您的供應商/寶石文件夾,並轉到** /供應商/寶石/ public_activity-1.4.2/lib目錄/ public_activity/ORM/active_record/activity.rb **並在那裏應用'acts_as_votable'並檢查它是否有效。 – Abhi
使用activity.likes.size或更好顯示使用<%= number_to_human(activity.likes.size)%> – Milind
更改它將返回此錯誤:undefined方法'likes' –