Home > Software design >  Couldn't find User without an ID
Couldn't find User without an ID

Time:12-14

Hi I am new to rails and I have just started making a facebook clone and I am starting out with the friends function, however I am getting the error ActiveRecord::RecordNotFound in UsersController#show Couldn't find User without an ID any help would be much appreciated

here is a picture of the error enter image description here

users_controller.rb

class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_user

def show
    
end

def friend
    current_user.sent_follow_request_to(@user)
    redirect_to root_path
end

def unfriend
    current_user.unfollow(@user)
    @user.unfollow(current_user)
    redirect_to root_path
end

def accept
    current_user.accept_follow_request_of(@user)
    current_user.send_follow_request_to(@user)
    @user.accept_follow_request_of(current_user)
    redirect_to root_path
end

def decline
    current_user.decline_follow_request_of(@user)
    redirect_to root_path
end

def cancel
    current_user.remove_follow_request_for(@user)
    redirect_to root_path
end

private

def set_user
    @user = User.find(params[:id])

end


end

user.rb

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :validatable

followability

def unfollow(user)
followerable_relationships.where(followable_id: user.id).destroy_all
end

end

routes.rb

 Rails.application.routes.draw do
 devise_for :users



 post 'users/:id/unfriend', to: 'users#unfriend', as: 'unfriend'
 post 'users/:id/friend', to: 'users#friend', as: 'follow'
 post 'users/:id/accept', to: 'users#accept', as: 'accept'
 post 'users/:id/decline', to: 'users#decline', as: 'decline'
 post 'users/:id/cancel', to: 'users#cancel', as: 'cancel'

 get 'users/:id', to: 'users#show', as: 'users'

 resources :users

 root 'users#show'
 # Define your application routes per the DSL in 
 https://guides.rubyonrails.org/routing.html

 # Defines the root path route ("/")
 # root "articles#index"
 end

CodePudding user response:

The problem is your root route:

root 'users#show'

You've defined your root web page, that is what will show up when the user requests /, as when they go to http://localhost:3000. It's set to the page which shows a specific user. This requires a User ID to do its job. But there is no ID provided.

Pick something else for your root page. users#index for example. Or your registration and sign on page. See the Devise docs for setting that up.

  • Related