【发布时间】: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
编辑
所以问题确实是因为我的观点是空洞的。
所以我想至少,我应该能够将<%= @feed_items %> 放在我的视图中,对吧?当我这样做时,我会在我的页面上收到以下输出:#<ActiveRecord::Relation::ActiveRecord_Relation_Thing:0x00000108ad3f50>
【问题讨论】:
-
不想生涩,但您的 Feed 项未显示,因为您说您的
myfeed.html.erb为空。在视图中使用您的@feed_items并以您想要的方式显示它们。如果没有渲染它们,则不会显示任何内容。 -
你没有生涩。你对我的问题给出了合理的回答。我会用另一个问题更新我的问题。 @NickVeys
标签: ruby-on-rails ruby rails-activerecord feed