简体   繁体   中英

Ch. 10.3.3 Ruby on Rails Tutorial Undefined Method 'any?'

I'm trying to put a feed of all the todo items on the front page when a user is logged in. However, @feed_items is not returning anything, even though feed is working. Here's the error:

undefined method `any?' for nil:NilClass

Here's the extracted source:

1: <% if @feed_items.any? %>
2:  <ol class="todos">
3:      <%= render partial: 'shared/feed_item', collection: @feed_items %>
4:  </ol>

Here's my Static Pages Controller:

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

Here's my 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

And here is my home page (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" %>

Thank you so much in advance!

Nil doesn't provide a method any? . A quick hack would be to use the try method:

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

@feed_items is nil, and you're trying to call any? on it. Nil doesn't have that method.

Your controller is "home", but your view is "index". It may be that your controller action isn't being run, and that @feed_items isn't being populated as a result. Try renaming home to index and see if that fixes it.

在create action中渲染'static_pages / home'之前,确保MicropostsController有@feed_items = []

See listing 11.48 in the Hartl Rails Tutorial. You need to add the following line of code to the 'create' method in the 'todos'controller: @feed_items = []. I had the same problem until I added this line. Now everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM