-
Notifications
You must be signed in to change notification settings - Fork 2
Rails error: Routing Error: uninitialized constant
If your error looks like ...
Routing Error
uninitialized constant ProductsController
... then rails can't find the controller being routed to.
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.
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
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
This is a living document. If you have anything to add, change or remove, please let us know. Or better yet, as it's a wiki, make the change yourself!