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