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

Creating the Friendship: NoMethodError: undefined method `empty?' for nil:NilClass

Hello guys!

I know we should face a lot of difficulties when trying to follow the old tutorials with Ruby 2.0 and Rails 4, but I'm surprised by some things that I believe should work the same in both versions.

First problem: I get no error or failure message when running a test that is posting a #create method that is non existent in my controller. How could that be?

My user friendships test file looks like this:

context "with no friend_id" do
   setup do
      post :create
   end
end

My user friendships controller looks like this:

class UserFriendshipsController < ApplicationController

    before_filter :authenticate_user!, only: [:new]

    def new
        if params[:friend_id]
          @friend = User.where(profile_name: params[:friend_id]).first
          raise ActiveRecord::RecordNotFound if @friend.nil?
          @user_friendship = current_user.user_friendships.new(friend: @friend)
        else
            flash[:error] = "Friend id is missing."
        end
      rescue ActiveRecord::RecordNotFound 
        render 'public/404.html', status: :not_found
    end
end

My test returns no error...

Second problem: When I run the test bellow, I get a No method error.

should "set the flash error message" do
   assert !flash[:error].empty?
end 

The error in my command line looks like this:

Finished tests in 0.196683s, 55.9276 tests/s, 71.1805 assertions/s.

  1) Error:
UserFriendshipsControllerTest#test: #create when logged in should set the flash error message. :
NoMethodError: undefined method `empty?' for nil:NilClass
    test/controllers/user_friendships_controller_test.rb:85:in `block (3 levels) in <class:UserFriendshipsControllerTest>'

11 tests, 14 assertions, 0 failures, 1 errors, 0 skips

Any help? Even from a simple and basic teaching point of view?

Thanks a lot!

2 Answers

On the second one it says that undefined empty for nil means that the flash error message is not being called or not defined. I recommend watching the video closely again or downloading the project files to compare. When I did this series I made silly errors in typing or missed something jason said.

I can try to help more if you can tell me what is on line 85 of user_friendship_controller_test as per the error: test/controllers/user_friendships_controller_test.rb:85

Hello Brandon thanks for trying to help. Actually I went through the video once more and find out some simple typo.