Home > Back-end >  rake aborted! NameError: uninitialized constant Products
rake aborted! NameError: uninitialized constant Products

Time:11-10

Newbie on RoR and a little confused on whats happening.

I am following the tutorial here: https://www.javatpoint.com/ruby-on-rails-crud to create a crud application. On step 17 I have to run the "rake db:migrate" in my terminal. Each time I get the error message "NameError: uninitialized constant Products mount Products::ProductsAPI => '/api/products'"

I worked through suggestion here:https://stackoverflow.com/questions/35978598/rake-aborted-nameerror-uninitialized-constant-users in an older post. All of the other classes and modules are labelled as "Products" except for the Model product.rb (\crud\app\models\product.rb). I copied the code exactly and updated the gems using "gem update --system" incase it was the Grape gem causing the issue. I'll share the files below:

My Products Controller

class ProductsController < ApplicationController   
   
# GET method to get all products from database   
def index   
  @products = Product.all   
end   
 
# GET method to get a product by id   
def show   
  @product = Product.find(params[:id])   
end   
 
# GET method for the new product form   
def new   
  @product = Product.new   
end   
 
# POST method for processing form data   
def create   
  @product = Product.new(product_params)   
  if @product.save   
    flash[:notice] = 'Product added!'   
    redirect_to root_path   
  else   
    flash[:error] = 'Failed to edit product!'   
    render :new   
  end   
end   
 
 # GET method for editing a product based on id   
def edit   
  @product = Product.find(params[:id])   
end   
 
# PUT method for updating in database a product based on id   
def update   
  @product = Product.find(params[:id])   
  if @product.update_attributes(product_params)   
    flash[:notice] = 'Product updated!'   
    redirect_to root_path   
  else   
    flash[:error] = 'Failed to edit product!'   
    render :edit   
  end   
end   
 
# DELETE method for deleting a product from database based on id   
def destroy   
  @product = Product.find(params[:id])   
  if @product.delete   
    flash[:notice] = 'Product deleted!'   
    redirect_to root_path   
  else   
    flash[:error] = 'Failed to delete this product!'   
    render :destroy   
  end   
end   
 
# we used strong parameters for the validation of params   
def product_params   
  params.require(:product).permit(:name, :price, :old_price, :short_description, :full_description)   
end   
 
end  

My Product Model:

class Product < ApplicationRecord   
    validates :name, presence: true
    validates :price, presence: true, numericality: {:greater_than => 0}
    validates :short_description, presence: true   
end  

My migrate db table:

class CreateProducts < ActiveRecord::Migration[7.0]
  def change
    create_table :products do |t|
      t.string :name
      t.decimal :price
      t.text :short_description
      t.text :full_description
      t.timestamps
    end
  end
end

My routes file:

Rails.application.routes.draw do
  get 'products/index'
  get 'products/show'
  get 'products/new'
  get 'products/create'
  get 'products/edit'
  get 'products/update'
  get 'products/destroy'
 
   
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
  resources :products  
  root 'products#index'  
   
  # Defines the root path route ("/")
mount Products::ProductsAPI => '/api/products'
  
  # root "articles#index"
end

I followed the steps to the letter but I am unable to run the rake db:migrate.

  • I changed the migration filename and class name so that they were matching - didnt run

  • I updated the location of the route in the routes file, did not run and got the same error.

  • I reviewed and updated the code to match exactly the steps from the javtpoint tutorial and its not running.

  • I tried running the terminal in admin incase it helped

  • I put the full location of the folder in the routes file instead of "ProductsAPI => '/api/products'"I had C:\Users\Desktop\CRUD\CRUD\crud\app\api\products

  • Also, updated the gems and and ran bundle install after. Everything is working as designed as far as step 17 "rake db:migrate".

Really appreciate your help!

CodePudding user response:

It's not able to find this file from step 15:

app/api/products/products_api.rb

You can quickly verify this from the Rails console:

$ rails console
[1] development(main)> Products::ProductsAPI
NameError: uninitialized constant Products

Does this file exist?

Also, I don't believe that the config.autoload_paths configuration from Step 4 is necessary these days. Any custom directories under /app are automatically loaded in Rails 7:

https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#config-autoload-paths

Note: This tutorial looks like it's based off of Rails 4 or 5 from around 2015, and you are using Rails 7. You might run into other issues trying to implement this tutorial in Rails 7. I would recommend finding a more modern tutorial.

  • Related