1

I've been following the Railscast video called 'Public Activity' and I've gotten stuck on the following error:

undefined method `post'

It happens when I try to output what post that have been created.

My partial looks like this:

_create.html.erb

added comment to 
 <% if activity.trackable %>
  <%= link_to activity.trackable.post.title, activity.trackable.post %>
 <% else %>
  which has since been removed 
 <% end %>

I can see that my activities table in fact stores all new posts, and creates the right owner_id and the trackable_type is Post with key Post.create

My post.rb model looks like this:

class Post < ActiveRecord::Base
 include PublicActivity::Model
 tracked owner: Proc.new{ |controller, model| controller.current_user }

 belongs_to :user
 has_many :comments, dependent: :destroy
end

This is my activity stream (newsfeeds/index.html.erb)

<h2>Stream</h2>
 <% @activities.each do |activity| %>
 <%= link_to activity.owner.fullname, root_url %> 
 <%= render_activity activity %>
<% end %>

When I remove the partial. I do successfully print out the 'owner.fullname' as a side note.

My newsfeeds_controller.rb:

class NewsfeedsController < ApplicationController
 def index
    @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.friend_ids, owner_type: "User")
 end 

 def show
 end 
end

What I am trying to do is the same as what he is trying to do on Railscast, almost. I wish to print out the title of the post.

My post has a :title and :content

sja
  • 347
  • 1
  • 10
  • What does the `render_activity` method look like – CWitty Jul 10 '14 at 19:24
  • it basically calls the personal_activity/post/_create.html.erb for example. It's to specify the feed based on the action say create is "created comment" whilst _destroy would be "deleted comment" in the feed – sja Jul 10 '14 at 20:16
  • Without seeing the actual code it is hard to tell, I imagine that the variable is not making it all the way to the partial. Check to make sure that the `render_activity` method also passes the `activity` variable and that it continues down the line until it reaches the partial. – CWitty Jul 10 '14 at 20:25
  • @CWitty tried using it without the partial and it has the same error. However I changed the code to: skrev et nytt innlegg <% if activity.trackable %> <%= activity.owner_id %> <%= link_to activity.trackable_type, activity.trackable %> <% else %> which has since been removed <% end %> and now it displays the post as a link. However, how do I show the actual content? it doesn't seem to get anything related to the post model.. – sja Jul 10 '14 at 22:37
  • When you change the `link_to` to use properties of the `trackable` it doesnt work? Is it giving you errors and if so can you include them? – CWitty Jul 11 '14 at 04:05

1 Answers1

2

I managed to figure out. The problem was that in the:

link_to activity.trackable.post.title 

I was actually asking this in clear code:

link_to activity.trackable.post.post.title

because the trackable stores the model, in this case post. So I don't have to write it. I just had to remove that and add 'title' on the end. Thus I call post.title.

Correct and working way is:

link_to activity.trackable.title
sja
  • 347
  • 1
  • 10