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 Building Social Features in Ruby on Rails Adding State What is a State Machine?

Matthew Pierce
Matthew Pierce
15,779 Points

undefined local variable or method `user' for #<UserNotifier:0x482a648>

I'm stuck on the final test we write in this video, receiving the following error:

1) Error:
UserFriendshipTest#test_: #send_request_email should send an email. :
NameError: undefined local variable or method `user' for #<UserNotifier:0x482a648>
    app/mailers/user_notifier.rb:10:in `friend_requested'
    app/models/user_friendship.rb:12:in `send_request_email'
    test/models/user_friendship_test.rb:35:in `block (3 levels) in <class:UserFriendshipTest>'
    test/models/user_friendship_test.rb:34:in `block (2 levels) in <class:UserFriendshipTest>'

user_notifier:

class UserNotifier < ApplicationMailer

    def friend_requested(user_friendship_id)
        user_friendship = UserFriendship.find(user_friendship_id)

        @user = user_friendship.user
        @friend = user_friendship.friend

        mail to: @friend.email,
                    subject: "#{user.first_name} wants to be friends on Treebook"
    end
end

user_friendship:

class UserFriendship < ActiveRecord::Base
    belongs_to :user
    belongs_to :friend, class_name: "User", foreign_key: "friend_id"

    after_initialize :set_initial_state
    def set_initial_state
      self.state ||= :pending
    end


    def send_request_email
        UserNotifier.friend_requested(id).deliver_now
    end
end

and lastly, my test:

context "#send_request_email" do
        setup do
            @user_friendship = UserFriendship.create user: users(:matthew), friend: users(:mike)
        end

        should "send an email" do
            assert_difference "ActionMailer::Base.deliveries.size", 1 do
                @user_friendship.send_request_email
            end
        end
    end

Any help is appreciated!

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I think this method is the problem:

def friend_requested(user_friendship_id)
        user_friendship = UserFriendship.find(user_friendship_id)

        @user = user_friendship.user
        @friend = user_friendship.friend

        mail to: @friend.email,
                    subject: "#{user.first_name} wants to be friends on Treebook"
    end

You're defining @user variable, but the you try to use a user variable, which you did not define anywhere, in the mail subject. These two have to be the same for it to work.

Matthew Pierce
Matthew Pierce
15,779 Points

Thanks! I'm not sure how I missed that.