J Seabolt J Seabolt - 8 days ago 7
Ruby Question

uninitialized constant CustomersController - Rails

I'm having trouble with my routing in a Rails app. I get this error when I try to follow a link to the new_customer_path:

uninitialized constant CustomersController


This is the link I am trying to follow. It is on the "new" page for my Movies controller. Here is the relevant portion of the "new" page:

<div class="row">
<div class="col-xs-12">
<hr />
<%= link_to "Add Customer", new_customer_path, class: 'white' %>
</div>
</div>


Customer Controller:

class CustomerController < ApplicationController
def new
@customer = Customer.new
end

def create
@customer = Customer.new(customer_params)
if @customer.save
redirect_to new_customer_path
end
end

private

def customer_params
params.require(:customer).permit(:fname, :lname, :telephone, :email)
end

end


Routes:

Rails.application.routes.draw do
resources :customers

resources :movies do
resources :rentals
end
root 'movies#new'
end


Customer Model:

class Customer < ApplicationRecord
has_many :rentals
end


Any thoughts/tips would be very much appreciated!

Dbz Dbz
Answer

Could it be that you're missing an s?

class CustomerController < ApplicationController should be class CustomersController < ApplicationController

Rails is convention over configuration which means:

  • Models are singular: Object
  • Controllers are plural: ObjectsController

This can be changed if you would like, but I would stick with convention unless you have a good reason.