Public Activity with Acts As Votable

206 views
Skip to first unread message

Richard Coy

unread,
Apr 28, 2015, 9:21:00 PM4/28/15
to public-...@googlegroups.com
I'm trying to add Acts As Votable to my Public Activity Model but I get the following error when I try to view the page:

undefined method `get_upvotes' for nil:NilClass

It chokes on this line:

 <span class="badge"><%= @activity.get_upvotes.size %></span>

I'm pretty new to Rails so this is throwing me for a loop. Here is the related code:

View Code

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

Model Code

class Activity < ActiveRecord::Base
    acts_as_votable
end


Controller Code

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.upvote_from current_user
    redirect_to activities_path
  end

end


Thanks for any help you can offer!

Rich

Piotrek Okoński

unread,
Apr 29, 2015, 5:29:39 AM4/29/15
to Richard Coy, public-...@googlegroups.com
Your model here:

class Activity < ActiveRecord::Base
    acts_as_votable
end

completely skips PublicActivity. You should inherit from PublicActivity::Activity, not ActiveRecord::Base if you want to extend PA.

Also @activity is nil, because you are not assigning to that variable anywhere.

--
You received this message because you are subscribed to the Google Groups "Public Activity" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-activi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Janusz M

unread,
Aug 26, 2016, 5:53:02 AM8/26/16
to Public Activity, rich....@gmail.com
So if I'd like to extend PublicActivity::Activity with, let's say, an association - has_many :comments - I should create a new model that inherits from PA and add association to that model?

Can I still keep using the same db table (activities) ? And create new activities in the same way as before, calling activities.create on trackable or something along those lines?

```@activity = @trackable.activities.find_or_initialize_by owner: @owner, recipient: @recipient```

Piotrek Okoński

unread,
Aug 26, 2016, 6:52:41 AM8/26/16
to Janusz M, Public Activity, Richard Coy
Inheriting will not work since P_A will not use your model, it uses its own internally. Use class_eval as described here https://github.com/chaps-io/public_activity/wiki/%5BHow-to%5D-Use-custom-fields-on-Activity#setup

To unsubscribe from this group and stop receiving emails from it, send an email to public-activity+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages