2013-06-17 3 views
1

사용자가 로그인하면 모든 할 일 목록의 피드를 첫 페이지에 넣으려고합니다. 그러나 피드가 작동하고 있어도 @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)입니다!

답변

3

@feed_items은 nil이므로 any?을 호출하려고합니다. Nil에는 해당 메소드가 없습니다.

컨트롤러는 "집"이지만보기는 "색인"입니다. 컨트롤러 동작이 실행되고 있지 않고 그 결과로 @feed_items이 채워지지 않은 것일 수 있습니다. home의 이름을 index으로 변경하고 수정했는지 확인하십시오.

+0

두 가지가 있습니다. 내 페이지의 이름을 바꿔서 올바른 페이지에 맞게 색인을 생성해야했습니다. 그리고 나서 저는 실제로 홈 컨트롤러를 가지고 있고 잘못된 컨트롤러에 제 방법을 적용하고 있다는 것을 깨달았습니다. 도와 주셔서 감사합니다! – user2494736

6

Nilany?을 제공하지 않습니다. 의 조치를 만들 'static_pages/집'을 렌더링하기 전에

<% if @feed_items.try(:any?) %> 
1

이 MicropostsController이 @feed_items = []가 있음을 확인 : 빠른 해킹은 try 방법을 사용하는 것입니다.

1

Hartl Rails 튜토리얼의 11.48을 참조하십시오. 'todos'컨트롤러의 'create'메소드에 다음 코드 행을 추가해야합니다 : @feed_items = []. 이 줄을 추가하기 전까지 같은 문제가있었습니다. 이제 모든 것이 잘됩니다.

관련 문제