为什么我的饲料项目没有显示?

时间:2013-12-04 18:24:32

标签: ruby-on-rails ruby rails-activerecord news-feed

我已经复制并粘贴了Michael Hartl教程中的代码并将其更改为符合我的需求。我的应用程序目前有一个跟踪系统,用户应该可以导航到一个页面,在该页面中他们只能看到自己和他们关注的帖子。

我对myfeed.html.erb的观点是为空,因为我不确定要放入什么(或者根本不属于任何内容)。 Michael Hartl似乎没有在他的教程中向主页添加任何内容。

这是我的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

这是我的事物模型:

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

以下是我的用户模型的Feed方法:

 def feed
    Thing.from_users_followed_by(self)
 end

修改

所以问题确实是因为我的观点是空洞的。

所以我至少想到,我应该能够将<%= @feed_items %>放在我看来,对吧?当我这样做时,我会在页面上收到此输出:#<ActiveRecord::Relation::ActiveRecord_Relation_Thing:0x00000108ad3f50>

1 个答案:

答案 0 :(得分:2)

不得不像这样迭代:

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