Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Still having issues with undefined method

Hey guys, I posted on here yesterday and still haven't been able to figure out the problem..

Here is the error I'm getting while running the test for ActivitiesControllerTest

1) Error:
test_should_get_index(ActivitiesControllerTest):
NoMethodError: undefined method `friends' for nil:NilClass
/Users/justinlicata/Documents/ruby/treebook/app/controllers/activities_controller.rb:3:in `index'

Here is the the Activities Controller

class ActivitiesController < ApplicationController
  def index
    friend_ids = current_user.friends.map(&:id)
    @activities = Activity.where("user_id in (?)", friend_ids.push(current_user.id)).order("created_at desc").all
  end
end

and Here is the Activities Controller Test

require 'test_helper'
class ActivitiesControllerTest < ActionController::TestCase
  test "should get index" do
    get :index
    assert_response :success
  end
end

Lastly, while trying to access the Activity Feed Page, I get this errror

undefined method `friends' for nil:NilClass

and

undefined method `underscore' for nil:NilClass

Here are my files on Github https://github.com/licatajustin/treebook

Anyone know what I've missed? Thank you in advance for the help

4 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Justin,

Jason Seifer is on holiday right now. I'll see if I can help you.

It looks like, current_user, is not being set in the test. You need to make sure you have a user that's logged in to make sure your test passes.

Because current_user is nil, or undefined, the friends method that's being called will through the "undefined method friends' for nil:NilClass" error. So theassert_response :success` will fail because testing that route will actually throw an error or status code 500.

I believe this video will tell you how to use devise to log in during your test. Regards
Andrew

What was weird Andrew is that all my code was correct. I cloned it all into another directory and it worked fine!

Thank you for your help though

Very. have a good one

alex morrison
alex morrison
135 Points

I have the same issue. Though, when someone I'm following creates a post, it shows up in the activity index page. Yet, when someone I'm following follows another person, I get the "undefined method `underscore' for nil:NilClass".