CJK
CJK

Reputation: 39

Couldn't find User without an ID rails error

I am new to rails and i keep getting this error

Couldn't find User without an ID

from:

    class UserController < ApplicationController
     def show
      @user = User.find(params[:id])
     end
    end

this is what i have;

model/user.rb

  class User < ActiveRecord::Base

      devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
      validates :name, presence: true
      validates :email, presence: true

      has_many :listings, dependent: :destroy

      has_many :purchasing, class_name: "Transaction", foreign_key: "buyer_id", dependent: :destroy
      has_many :sell, class_name: "Transaction", foreign_key: "seller_id", dependent: :destroy
 

      has_many :purchased, class_name: "Archive", foreign_key: "buyer_id", dependent: :destroy
      has_many :sales, class_name: "Archive", foreign_key: "seller_id", dependent: :destroy

      has_many :selling_rooms, class_name: "Room", foreign_key: "seller_id", dependent: :destroy
      has_many :buying_room, class_name: "Room", foreign_key: "buyer_id", dependent: :destroy

     def can_buy?(listing_price)
      if self.points >= listing_price
        true
      else
        false
      end
     end

     def withdraw(listing_price)
      self.points -= listing_price
     end


    def purchasing_list
     purchasing.includes(:seller, :listing)
    end

    def purchased_list
     purchased.includes(:seller, :listing)
    end

    def sell_list
     sell.includes(:seller, :listing)
    end

    def sales_list
     sales.includes(:seller, :listing)
    end
  end

resources

    resources :users

I looked around but all i could find was something saying that it is looking for a resource that doesn't exist.

Upvotes: 1

Views: 3235

Answers (4)

JK Gunnink
JK Gunnink

Reputation: 153

Check yo console!

In the parameters that are sent through in the request that is blowing up, have a look at exactly what the id your sending through is. Is it a user_id? is it an id? Your controller is expecting an id so make sure you are passing it one.

If you're passing an entire object you will need to specify. Eg. If you're passing through a user you'll need to pass through a user.id instead because your controller is expecting an ID which is typically a number. user will give the whole object, user.id will give just the number value, which is the ID in this case. Then with that number the controller action can find the relevant user in the database.

Upvotes: 0

Sravan
Sravan

Reputation: 18647

Some where in your form which redirects you to the users show page you should send the id or send the object itself.

<%= link_to 'Show', user_path(user) %>

or

<%= link_to 'Show', user_path(user.id) %>

Upvotes: 2

Pooja Mokariya
Pooja Mokariya

Reputation: 2120

It seems that params[:id] not present.

first try to check with putting in

class UserController < ApplicationController
 def show
   logger"-----#{params[:id]}---"
    @user = User.find(params[:id])
 end
end

if its empty then pass id from the link from where you getting this error

ex. localhost:3000/users/1

here params[:id] == 1 must be

Upvotes: 2

Navin
Navin

Reputation: 924

Please check your params, As I know you are getting params[:id] = nil.

Try to pass :id in your params or you can check it by hard-coding it.

Upvotes: 0

Related Questions