Dylan Richards
Dylan Richards

Reputation: 708

Why aren't my feed items showing?

I have copied and pasted code from Michael Hartl's tutorial and changed it to fit my needs. My application currently has a follow system where users should be able to navigate to a page where they only see posts from themselves and those who they are following.

My view for myfeed.html.erb is empty because I'm not sure what to put in it (or if anything belongs in there at all). Michael Hartl doesn't seem to be adding anything to the home page in his tutorial.

Here is my PagesController:

class PagesController < ApplicationController
  def home
  end

  def about
  end

  def myfeed
    if signed_in?
      @thing  = current_user.things.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end
end

Here is my Thing model:

class Thing < ActiveRecord::Base
  belongs_to :user

  default_scope -> { order('created_at DESC') }

  has_attached_file :image, :styles => { :large => '500x500>', :medium => '300x300>', :thumb => '100x100>' }

  validates :image, presence: true
  validates :title, presence: true

  # Returns microposts from the users being followed by the given user.
  def self.from_users_followed_by(user)
    followed_user_ids = "SELECT followed_id FROM relationships
                         WHERE follower_id = :user_id"
    where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
          user_id: user.id)
  end
end

Here is my User model's feed method:

 def feed
    Thing.from_users_followed_by(self)
 end

EDIT

So the problem was, indeed, because my view was empty.

So I figured at the very least, I should be able to put <%= @feed_items %> in my view, right? When I do, I receive this output on my page: #<ActiveRecord::Relation::ActiveRecord_Relation_Thing:0x00000108ad3f50>

Upvotes: 0

Views: 218

Answers (1)

Dylan Richards
Dylan Richards

Reputation: 708

Had to iterate through like so:

<div id="things" class="transitions-enabled">
  <% @feed_items.each do |thing| %>
    <div class='panel panel default'>
      <div class="box">
        <%= link_to image_tag(thing.image.url(:medium)), thing %>
        <div class='panel-body'>
        <% if thing.link.blank? %>
        <strong><%= thing.title %></strong>
        <% else %>
        <strong><%= link_to thing.title, "http://#{thing.link}"%></strong>
        <% end %>

        <p><%= thing.description %></p>
        By <%= link_to thing.user.username, user_path(thing.user) %>

        <% if thing.user == current_user %>
          <%= link_to edit_thing_path(thing) do %>
          <span class='glyphicon glyphicon-edit'></span>
        <% end %>
        <%= link_to thing_path(thing), method: :delete, data: { confirm: 'Are you sure?' } do %>
          <span class='glyphicon glyphicon-trash'></span>
        <% end %>
        </div>
        <% end %>
        </div>
      </div>
    <% end %>
</div>

Upvotes: 2

Related Questions