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 Using AJAX Implementing AJAX: Part 3

I'm stuck! Getting NoMethodError in UserFriendhsipsController

I keep getting a 'NoMethodError in UserFriendshipsController'. undefined method `id' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x0

This is the line of code in my edit section: @user_friendship = current_user.user_friendships.where(friend_id: @friend.id).decorate

Thank you in advance!

user_friendships controller

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

   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_mutual_friendship!
     @user_friendship.friend.user_friendships.find_by(friend_id:   current_user.id).accept_mutual_friendship!
     flash[:success] = "You are now friends with #{@user_friendship.friend.name}"
     redirect_to user_friendships_path
     else
     flash[:error] = "That friendship could not be accepted"
     end
   end

   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 = UserFriendship.request(current_user, @friend)
    respond_to do |format|
    if @user_friendship.new_record?
      format.html do
        flash[:error] = "There was a problem creating this friend request."
        redirect_to user_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 user_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 edit
  @friend = User.where(user: params[:id])
  @user_friendship = current_user.user_friendships.where(friend_id:    @friend.id).decorate
 end

  def destroy
   @user_friendship = current_user.user_friendships.find(params[:id])
  if @user_friendship.destroy
  flash[:success] = "Your friendship was deleted"
  end  
   redirect_to user_friendships_path
  end

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

User_friendship Model

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

     after_destroy :delete_mutual_friendship!

     state_machine :state, initial: :pending do

     after_transition on: :accept, do: [:send_acceptance_email, :accept_mutual_friendship!]

     state :requested

       event :accept do
        transition any => :accepted
       end
    end

javascript/user_friendships.js

 $(document).ready(function() {

$('#add-friendship').click(function(event) {
    event. preventDefault();
    var addFriendshipBtn = $(this);
    $.ajax({
        url: Routes.user_friendships_path({user_friendship: { friend_id:    addFriendshipBtn.data('friendId') }}), 
        dataType: 'json',
        type: 'POST',
        success: function(e) {
            addFriendshipBtn.hide();
            $('#friend-status').html("<a href='#' class='btn btn-success'>Friendship Requested</a>");
        }

    });
     });


  });

1 Answer

I got it. It should read for my code...

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