【问题标题】:Why aren't my feed items showing?为什么我的 Feed 项没有显示?
【发布时间】:2013-12-04 18:24:32
【问题描述】:

我复制并粘贴了 Michael Hartl 教程中的代码,并根据我的需要对其进行了更改。我的应用程序目前有一个关注系统,用户应该能够导航到一个页面,在那里他们只能看到他们自己和他们关注的人的帖子。

我对 myfeed.html.erb 的看法是empty,因为我不确定要在其中放入什么(或者是否有任何东西属于其中)。 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

这是我的用户模型的提要方法:

 def feed
    Thing.from_users_followed_by(self)
 end

编辑

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

所以我想至少,我应该能够将&lt;%= @feed_items %&gt; 放在我的视图中,对吧?当我这样做时,我会在我的页面上收到以下输出:#&lt;ActiveRecord::Relation::ActiveRecord_Relation_Thing:0x00000108ad3f50&gt;

【问题讨论】:

  • 不想生涩,但您的 Feed 项未显示,因为您说您的 myfeed.html.erb 为空。在视图中使用您的@feed_items 并以您想要的方式显示它们。如果没有渲染它们,则不会显示任何内容。
  • 你没有生涩。你对我的问题给出了合理的回答。我会用另一个问题更新我的问题。 @NickVeys

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


【解决方案1】:

必须像这样迭代:

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

【讨论】:

    猜你喜欢
    • 2017-05-12
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多