Skip to content

Rails error: Routing Error: uninitialized constant

Sean Lerner edited this page Apr 12, 2017 · 3 revisions

If your error looks like ...

Routing Error
uninitialized constant ProductsController

... then rails can't find the controller being routed to.

How To Fix

Step 1. Create a controller

Check your app/controllers folder. If you don't see a file for your controller, then use the rails generator to create a controller.

For example, from your command line:

rails generate controller products

Replace products with the controller you desire. Note that your controller name is likely pluralized.

Step 2. Check the name of your controller

Ensure that your controller is defined properly. Open the .rb file and at the beginning, you should see the class definition.

This is a valid controller definition:

class ProductsController < ApplicationController

Ensure that:

  • the case is correct: the controller name should be lowercase, except for the first letter and the C in controller
  • there are no underscores: ruby class names shouldn't have underscores
  • the first part of the controller name is pluralized

Here are some poorly defined controller names:

class productsController  < ApplicationController # P should be capitalized
class Productscontroller  < ApplicationController # C should be capitalized
class Products_Controller < ApplicationController # there should be no underscore
class ProductController   < ApplicationController # Product should be pluralized

Step 3. Check the filename for your controller

Controller filenames should be all lowercase and with an underscore separating the specific controller and the word controller. It should reside in your app/controllers folder.

This is a valid controller filename (incluing path):

app/controllers/products_controller.rb

Here are some invalid filenames (including path):

app/controllers/Products_controller.rb          # Products should not be capitalized
app/controllers/products_Controller.rb          # Controller should not be capitalized
app/controllers/productscontroller.rb           # Missing underscore
app/controllers/ProductsController.rb           # All of the above
app/products_controller.rb                      # Not in the correct folder
app/controllers/concerns/products_controller.rb # Not in the correct folder
app/controllers/product_controller.rb           # product should be pluralized
Clone this wiki locally