Dena
Dena

Reputation: 119

undefined method `decorate' for nil:NilClass

I am getting the below error

NoMethodError in UserFriendshipsController#edit undefined method `decorate' for nil:NilClass

I'm not sure how to resolve it.

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]).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.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.find(params[:id])
    @user_friendship = current_user.user_friendships.find_by(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

Upvotes: 0

Views: 1413

Answers (1)

Jirico
Jirico

Reputation: 1262

The problem is the query:

@user_friendship = current_user.user_friendships.where(friend_id: @friend.id).first

It is returning nil. Check if

  • The id you are getting from params[:id] is right
  • There is a current_user user_friendship relation with the passed @friend.id.

EDIT:

Check if the preceding query is correct:

@friend = User.where(params[:id]).first

As ABMagil said, this is not correct. Actually, i just tested in my console and it throws an exception if I search like:

u = User.where('1').first

Because it expects the field you are searching. Try to change to:

@friend = User.find(params[:id])

.find searchs for the id field and returns the single record. If you want to find a single record that is not the id, use:

@friend = User.find_by(my_table_field: params[:id])

Upvotes: 3

Related Questions