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!

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

undefined method `requested?' for nil:NilClass

Help Please! I keep getting the error:

NoMethodError in UserFriendships#edit

undefined method `requested?' for nil:NilClass.

I am working through the 'Accepting Friendships' tutorial and I am towards the very end of the video. When I click on my 'delete request' link which should take me to my user_friendship/edit page I get the error above.

Please see my code below. Thank you in advance.

user_friendships/edit

 <div class="page-header">
         <h1> Viewing Friendship </h1>
 </div>

 <% if @user_friendship.requested? %>
     <h3> Do you really want to be friends with <%= @friend.name %>?</h3>
 <% end %>


 <div class="form-actions">
     <% if @user_friendship.requested? %>
    <%= form_for @user_friendship, url: accept_user_friendship_path(@user_friendship), method:
        :put do |form| %>
        <%= submit_tag "Accept Friendship", class: 'btn btn-primary' %>
         <% end %>
     <% end %>
 </div> 

User_friendship/controller

     class UserFriendshipsController < ApplicationController
    before_filter :authenticate_user!

    def new
      if params[:friend_id]
        @friend = User.find(params[:friend_id])
        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.find(params[:user_friendship][:friend_id])
        @user_friendship = current_user.user_friendships.new(friend:  @friend)
        if @user_friendship.save
          flash[:success] = "You are now friends!"
        else
          flash[:error] = "There was a problem."
        end
        redirect_to user_path(@friend)
      else
        flash[:error] = "Friend required"
        redirect_to root_path
      end
    end


    def index
      @user_Friendships = current_user.user_friendships.all
    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.name}"
      else
        flash[:error] = "That friendship could not be accepted"
       redirect_to user_friendships_path
    end


     def edit
       @user_friendship = current_user.user_friendships.find(params [:id])
       @friend = @user_friendship.friend
     end

     def user_friendship
     params.require(:user_friendship).permit(:user_id, :friend_id, :user, :friend, :state, :user_friendship)
     end



  end
  end

2 Answers

I was able to get it to display by simply removing requested all together. I'm not sure why as I followed along with the tutorial. Well at least it works now. Hopefully removing it doesn't cause errors elsewhere.

Can you post the code of the delete link you select that generates the error?