当用户登录时,我试图在首页上放置所有待办事项的提要。但是,@feed_items 没有返回任何内容,即使提要正在工作。这是错误:
未定义的方法“任何?” 对于零:NilClass
这是提取的来源:
1: <% if @feed_items.any? %>
2: <ol class="todos">
3: <%= render partial: 'shared/feed_item', collection: @feed_items %>
4: </ol>
这是我的静态页面控制器:
class StaticPagesController < ApplicationController
def home
if signed_in?
@todo = current_user.todos.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
end
这是我的 User.rb
class User < ActiveRecord::Base
attr_accessible :email, :username, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
has_many :todos, :dependent => :destroy
validates :username, :presence => true, length: { maximum: 50 }
validates :password, :presence => true, length: { minimum: 6 }
validates :password_confirmation, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
def feed
# This is preliminary. See "Following users" for the full implementation.
Todo.where("user_id = ?", id)
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
这是我的主页(index.html.erb)
% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
</aside>
<div class="span8">
<h3>Todo Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center hero-unit">
<h1>Welcome to the To Do app!</h1>
<p>This app allows people to create a to do list for themselves on the web browser! HELLLZZZ YEEAAAHHH!!!</p>
<%= link_to "Sign Up Here!", signup_path, class: "btn btn-large btn-primary" %>
<%= link_to "Find your Friends (and other Users) Here!", users_path, class: "btn btn-large btn-primary" %>
非常感谢您!