3

I'm working on an app where people can like posts. I'm using the Public Activity gem and Acts As Votable.

I'm getting this error when I try to view the page:

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

get_upvotes is supposed to come with acts_as_votable. What am I doing wrong?

User Model

class User < ActiveRecord::Base
    acts_as_voter
end

Activity Model

class Activity < ActiveRecord::Base
    acts_as_votable
end

View Code

<% 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 %>

Activities 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

Thanks for any help you can offer. I'm at the edge of my Rails knowledge. :)

Rich Coy
  • 545
  • 1
  • 8
  • 24
  • Try extracting the **Public Activity** gem to your vendor/gems folder and goto **/vendor/gems/public_activity-1.4.2/lib/public_activity/orm/active_record/activity.rb** and apply `acts_as_votable` there and check if it works. – Abhi Apr 29 '15 at 11:23
  • use activity.likes.size or more better to display use <%=number_to_human( activity.likes.size) %> – Milind Apr 29 '15 at 12:07
  • Changing it returns this error: undefined method `likes' – Rich Coy Apr 29 '15 at 12:16

1 Answers1

0

The Activity model lives inside the PublicActivity module, meaning this should work.

class PublicActivity::Activity
  acts_as_votable
end
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102