ruby-on-railsrubypostman

NameError: uninitialized constant Api::V1::Item::ItemsController::Item


I have controller:

class Api::V1::Item::ItemsController < ApplicationController
  def index
     @items = Item.all 

     render json: @items
  end
end

and routes for this controller:

 namespace :api do
  namespace :v1 do
   namespace :item do
     resources :items
   end
  end
 end 

If I check this in postman, I obtain an error:

{
  "status": 500,
  "error": "Internal Server Error",
  "exception": "#<NameError: uninitialized constant Api::V1::Item::ItemsController::Item>",
 }

But if I modify controller`s method index like:

  def index
     render json: { check: 'Nice' }
  end

everything will be fine. Please explain to me what the problem is.


Solution

  • Try using absolute constant path ::Item instead of relative constant path Item.

    def index
      @items = ::Item.all
      # ...