当我尝试渲染我关注的用户的帖子时出错

时间:2014-08-24 21:56:17

标签: ruby-on-rails ruby ruby-on-rails-4

在我的应用中,我有一个页面,用于发布我关注的用户的状态。我是rails的新手,这个错误让我感到害怕。如果需要,我可以提供其他任何东西

pages_controller.rb

def home
  if signed_in?
    @hub_items = current_user.hub.paginate(page: params[:page])
  end
end

user.rb

def hub
  Timeline.from_users_followed_by(self)
end

timeline.rb

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

这是错误跟踪。

  

在2014-08-24 14:11:28 -0700开始获取127.0.0.1的“/”   由PagesController#home作为HTML处理     用户负载(3.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id”=?限制1 [[“id”,4]]   在257ms内完成500内部服务器错误

     

NoMethodError(未定义的方法`where time for Timeline:Class):

app/models/timeline.rb:47:in `from_users_followed_by'
app/models/user.rb:24:in `hub'
app/controllers/pages_controller.rb:5:in `home'
     

呈现C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_source.erb(4.0ms)     呈现C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb(4.0ms)     呈现C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb(5.0ms)     在救援/布局(646.0ms)内呈现C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb

2 个答案:

答案 0 :(得分:1)

class Timeline
  def self.do_something
    where(...) # is equivalent to self.where(...)
    # The `where` method should in your app be defined by ActiveRecord, 
    # and present in those classes that inherit from ActiveRecord::Base
    # (`class Post < ActiveRecord::Base; end). Since Timeline is no class
    # backed by a database table, you will want to load `Post.where(...)
    # or SomeModel.where(...) to display in your timeline.
  end
end

答案 1 :(得分:0)