user2494736
user2494736

Reputation: 23

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!

Upvotes: 2

Views: 2837

Answers (4)

Mark Simcoe
Mark Simcoe

Reputation: 19

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.

Upvotes: 1

feleck
feleck

Reputation: 11

Make sure that MicropostsController there's @feed_items = [] before rendering 'static_pages/home' in create action.

Upvotes: 1

wintermeyer
wintermeyer

Reputation: 8318

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

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

Upvotes: 7

Chris Heald
Chris Heald

Reputation: 62658

@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.

Upvotes: 3

Related Questions