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

Error - Using AJAX: Implementing AJAX - Part 3

I keep getting undefined method errors. The problem started 6:30 into the episode: Using AJAX: Implementing AJAX - Part 3 Thanks for the help :/, I feel stupid.

Error

C:\Sites\MSN>ruby -I test test/functional/user_friendships_controller_test.rb
Run options:

# Running tests:

................................EEE

Finished tests in 0.758043s, 46.1715 tests/s, 63.3209 assertions/s.

  1) Error:
test: when logged in should assign to friend. (UserFriendshipsControllerTest):
ActionView::Template::Error: undefined method `sub_message' for nil:NilClass
    C:/Sites/MSN/app/views/user_friendships/edit.html.erb:5:in `_app_views_user_
friendships_edit_html_erb__909172534_45010860'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:145:in `block in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:143:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_templ
ate'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:46:in `block in render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:54:in `render_with_layout'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:45:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:18:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:36:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:17:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:110:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/streaming.rb:225:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:103:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/renderers.rb:28:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/compatibility.rb:50:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:88:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/rendering.rb:16:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/core_ext/benchmark.rb:5:in `block in ms'

  2) Error:
test: when logged in should assign to user_friendship. (UserFriendshipsControlle
rTest):
ActionView::Template::Error: undefined method `sub_message' for nil:NilClass
    C:/Sites/MSN/app/views/user_friendships/edit.html.erb:5:in `_app_views_user_
friendships_edit_html_erb__909172534_45010860'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:145:in `block in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:143:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_templ
ate'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:46:in `block in render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:54:in `render_with_layout'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:45:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:18:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:36:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:17:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:110:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/streaming.rb:225:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:103:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/renderers.rb:28:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/compatibility.rb:50:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:88:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/rendering.rb:16:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/core_ext/benchmark.rb:5:in `block in ms'

  3) Error:
test: when logged in should get edit and return success. (UserFriendshipsControl
lerTest):
ActionView::Template::Error: undefined method `sub_message' for nil:NilClass
    C:/Sites/MSN/app/views/user_friendships/edit.html.erb:5:in `_app_views_user_
friendships_edit_html_erb__909172534_45010860'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:145:in `block in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:143:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_templ
ate'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:46:in `block in render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:54:in `render_with_layout'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:45:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:18:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:36:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:17:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:110:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/streaming.rb:225:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:103:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/renderers.rb:28:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/compatibility.rb:50:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:88:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/rendering.rb:16:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/core_ext/benchmark.rb:5:in `block in ms'

35 tests, 48 assertions, 0 failures, 3 errors, 0 skips

C:\Sites\MSN>

user_friendships_controller.rb

class UserFriendshipsController < ApplicationController
    before_filter :authenticate_user!
    respond_to :html, :json

    def index
        @user_friendships = current_user.user_friendships.all
    end

    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 required"
        end

      rescue ActiveRecord::RecordNotFound
          render file: 'public/404', status: :not_found
    end

    def create
        if params["user_friendship"] && params[:user_friendship].has_key?(:friend_id)
            @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
            @user_friendship = UserFriendship.request(current_user, @friend)
            respond_to do |format|
                if @user_friendship.new_record?
                    format.html do
                        flash[:error] = "There was a problem creating that friend request."
                        redirect_to profile_path(@friend)
                    end
                    format.json { render json: @user_friendship.to_json, status: :precondition_failed }
                else
                    format.html do
                        flash[:success] = "Friend request sent."
                        redirect_to profile_path(@friend)
                    end
                    format.json { render json: @user_friendship.to_json }
                end
            end
        else
            flash[:error] = "Friend required"
            redirect_to root_path
        end
    end

    def accept
        @user_friendship = current_user.user_friendships.find(params[:id])
        if @user_friendship.accept!
            flash[:success] = "You are now friends with #{@user_friendship.friend.first_name}"
        else
            flash[:error] = "That friendship could not be accepted."
        end
        redirect_to user_friendships_path
    end

    def edit
        @friend = User.where(profile_name: params[:id]).first
        @user_friendship = current_user.user_friendships.where(friend_id: @friend_id).first.decorate
    end

    def destroy
        @user_friendship = current_user.user_friendships.find(params[:id])
        if @user_friendship.destroy
            flash[:success] = "Friendship destroyed"
        end
        redirect_to user_friendships_path
    end
end

user_friendships_controller_test.rb

require 'test_helper'

class UserFriendshipsControllerTest < ActionController::TestCase
  context "#index" do
    context "when not logged in" do
      should "redirect to the login page" do
        get :index
        assert_response :redirect
      end

      context "when logged in" do
        setup do
          @friendship1 = create(:pending_user_friendship, user: users(:arthur), friend: create(:user, first_name: 'Pending', last_name: 'Friend'))
          @friendship2 = create(:accepted_user_friendship, user: users(:arthur), friend: create(:user, first_name: 'Active', last_name: 'Friend'))

          sign_in users(:arthur)
          get :index
        end

        should "get the index page without error" do
          assert_response :success
        end

        should "assign user_friendships" do
          assert assigns(:user_friendships)
        end

        should "display friend's names" do
          assert_match /Pending/, response.body
          assert_match /Active/, response.body
        end

        should "display pending information on a pending friendship" do
          assert_select "#user_friendship_#{@friendship1.id}" do
            assert_select "em", "Friendship is pending."
          end
        end

        should "display date information on an accepted friendship" do
          assert_select "#user_friendship_#{@friendship2.id}" do
            assert_select "em", "Friendship started #{@friendship2.updated_at}."
          end
        end
      end
    end

  context "#new" do
    context "when not logged in" do
      should "redirect to the login page" do
        get :new
        assert_response :redirect
      end
    end
  end

    context "when logged in" do
        setup do
            sign_in users(:arthur)
        end

        should "get new and return success" do
            get :new
            assert_response :success
        end

      should "should set a flash error if the friend_id params are missing" do
        get :new, {}
        assert_equal "Friend required", flash[:error]
      end

      should "should display friend's name" do
        get :new, friend_id: users(:sam)
        assert_match /#{users(:sam).full_name}/, response.body
      end

      should "assign a new user friendship" do
        get :new, friend_id: users(:sam)
        assert assigns(:user_friendship)
      end

      should "assign a new user friendship to the correct friend" do
        get :new, friend_id: users(:sam)
        assert_equal users(:sam), assigns(:user_friendship).friend
      end

      should "assign a new user friendship to the currently logged in user" do
        get :new, friend_id: users(:sam)
        assert_equal users(:arthur), assigns(:user_friendship).user
      end

      should "returns a 404 status if no friend is found" do
        get :new, friend_id:'invalid'
        assert_response :not_found
      end

      should "ask if you really want to request this friendship" do
        get :new, friend_id: users(:sam)
        assert_match /Do you really want to friend #{users(:sam).full_name}?/, response.body
      end

    end

  end

  context "#create" do
    context "when not logged in" do
      should "redirect to the login page" do
        get :new
        assert_response :redirect
        assert_redirected_to login_path
      end
    end
    context "when logged in" do
      setup do
        sign_in users(:arthur)
      end

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

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

        should "redirect to the site root" do
          assert_redirected_to root_path
        end
      end

      context "successfully" do
        should "create two user friendship objects" do
          assert_difference 'UserFriendship.count', 2 do
            post :create, user_friendship: { friend_id: users(:mike).profile_name }
          end
        end
      end

      context "with friend_id" do
        setup do
          post :create, user_friendship: { friend_id: users(:mike) }
        end

        should "assign a friend object" do
          assert assigns(:friend)
          assert_equal users(:mike), assigns(:friend)
        end

        should "assign a user_friendship object" do
          assert assigns(:user_friendship)
          assert_equal users(:arthur), assigns(:user_friendship).user
          assert_equal users(:mike), assigns(:user_friendship).friend
        end

        should "create a friendship" do
          assert users(:arthur).pending_friends.include?(users(:mike))
        end

        should "redirect to the profile page of the friend" do
          assert_response :redirect
          assert_redirected_to profile_path(users(:mike))
        end

        should "set the flash success message" do
          assert flash[:success]
          assert_equal "Friend request sent.", flash[:success]
        end
      end
    end
  end

  context "#accept" do
    context "when not logged in" do
      should "redirect to the login page" do
        put :accept, id: 1
        assert_response :redirect
        assert_redirected_to login_path
      end
    end

    context "when logged in" do
      setup do
        @friend = create(:user)
        @user_friendship = create(:pending_user_friendship, user: users(:arthur), friend: @friend)
        create(:pending_user_friendship, friend: users(:arthur), user: @friend)
        sign_in users(:arthur)
        put :accept, id: @user_friendship
        @user_friendship.reload
      end

      should "assign a user friendship" do
        assert assigns(:user_friendship)
        assert_equal @user_friendship, assigns(:user_friendship)
      end

      should "update the state to accepted" do
        assert_equal 'accepted', @user_friendship.state
      end

      should "have a flash sucess message" do
        assert_equal "You are now friends with #{@user_friendship.friend.first_name}", flash[:success]
      end
    end
  end

  context "#edit" do
    context "when not logged in" do
      should "redirect to the login page" do
        get :edit, id: 1
        assert_response :redirect
      end
    end
  end

  context "when logged in" do
    setup do
      @user_friendship = create(:pending_user_friendship, user: users(:arthur))
      sign_in users(:arthur)
      get :edit, id: @user_friendship.friend.profile_name
    end

    should "get edit and return success" do
      assert_response :success
     end

    should "assign to user_friendship" do
      assert assigns(:user_friendship)
     end

    should "assign to friend" do
      assert assigns(:friend)
    end
  end

  context "#destroy" do
    context "when not logged in" do
      should "redirect to the login page" do
        delete :destroy, id: 1
        assert_response :redirect
        assert_redirected_to login_path
      end
    end
    context "when logged in" do
      setup do
        @friend = create(:user)
        @user_friendship = create(:accepted_user_friendship, friend: @friend, user: users(:arthur))
        create(:accepted_user_friendship, friend: users(:arthur), user: @friend)

        sign_in users(:arthur)
      end

      should "delete user friendships" do
        assert_difference 'UserFriendship.count', -2 do
          delete :destroy, id: @user_friendship
        end
      end

      should "send the flash message" do
        delete :destroy, id: @user_friendship
        assert_equal "Friendship destroyed", flash[:success]
      end

    end
  end



end

3 Answers

Please help :(

I fixed my @friend_id spelling error, its actually @friend.id but now I'm getting a new error.

error

ruby -Itest test/functional/user_friendships_controller_test.rb
Run options:

# Running tests:

................................EEE

Finished tests in 0.758044s, 46.1715 tests/s, 63.3209 assertions/s.

  1) Error:
test: when logged in should assign to friend. (UserFriendshipsControllerTest):
ActionView::Template::Error: undefined method `requested?' for #<UserFriendshipD
ecorator:0x5321088>
    C:/Sites/MSN/app/views/user_friendships/edit.html.erb:8:in `_app_views_user_
friendships_edit_html_erb__988088858_43628928'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:145:in `block in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:143:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_templ
ate'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:46:in `block in render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:54:in `render_with_layout'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:45:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:18:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:36:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:17:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:110:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/streaming.rb:225:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:103:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/renderers.rb:28:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/compatibility.rb:50:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:88:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/rendering.rb:16:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/core_ext/benchmark.rb:5:in `block in ms'

  2) Error:
test: when logged in should assign to user_friendship. (UserFriendshipsControlle
rTest):
ActionView::Template::Error: undefined method `requested?' for #<UserFriendshipD
ecorator:0x53e7fb8>
    C:/Sites/MSN/app/views/user_friendships/edit.html.erb:8:in `_app_views_user_
friendships_edit_html_erb__988088858_43628928'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:145:in `block in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:143:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_templ
ate'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:46:in `block in render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:54:in `render_with_layout'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:45:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:18:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:36:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:17:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:110:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/streaming.rb:225:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:103:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/renderers.rb:28:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/compatibility.rb:50:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:88:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/rendering.rb:16:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/core_ext/benchmark.rb:5:in `block in ms'

  3) Error:
test: when logged in should get edit and return success. (UserFriendshipsControl
lerTest):
ActionView::Template::Error: undefined method `requested?' for #<UserFriendshipD
ecorator:0x591d3c8>
    C:/Sites/MSN/app/views/user_friendships/edit.html.erb:8:in `_app_views_user_
friendships_edit_html_erb__988088858_43628928'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:145:in `block in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/template.rb:143:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_templ
ate'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `block in instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications/instrumenter.rb:20:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/notifications.rb:123:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/abstract_renderer.rb:38:in `instrument'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:46:in `block in render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:54:in `render_with_layout'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:45:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/template_renderer.rb:18:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:36:in `render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_view/renderer/renderer.rb:17:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:110:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/streaming.rb:225:in `_render_template'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:103:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/renderers.rb:28:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/compatibility.rb:50:in `render_to_body'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
bstract_controller/rendering.rb:88:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/rendering.rb:16:in `render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/a
ction_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/li
b/active_support/core_ext/benchmark.rb:5:in `block in ms'

35 tests, 48 assertions, 0 failures, 3 errors, 0 skips

Jason helped me out on this one, https://teamtreehouse.com/forum/ajax-requested-error