사용자가 로그인하면 모든 할 일 목록의 피드를 첫 페이지에 넣으려고합니다. 그러나 피드가 작동하고 있어도 @feed_items는 아무 것도 반환하지 않습니다.Ch. 10.3.3 Ruby on Rails 튜토리얼 정의되지 않은 메소드 'any?'
1: <% if @feed_items.any? %>
2: <ol class="todos">
3: <%= render partial: 'shared/feed_item', collection: @feed_items %>
4: </ol>
가 여기 내 정적 페이지 컨트롤러 :
여기undefined method `any?' for nil:NilClass
추출 된 소스입니다 : 다음은 오류입니다
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+\-.][email protected][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
그리고있어 h 감수는
% 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" %>
가 사전에 너무 감사 나의 홈 페이지 (index.html.erb)입니다!
두 가지가 있습니다. 내 페이지의 이름을 바꿔서 올바른 페이지에 맞게 색인을 생성해야했습니다. 그리고 나서 저는 실제로 홈 컨트롤러를 가지고 있고 잘못된 컨트롤러에 제 방법을 적용하고 있다는 것을 깨달았습니다. 도와 주셔서 감사합니다! – user2494736