8legged
8legged

Reputation: 662

ActionController::RoutingError: uninitialized constant MicropostsController

Update: This was due to a misspelled file name

correct:
~/sample_app/app/controllers/microposts_controller.rb

incorrect:
~/sample_app/app/controllers/microposts_contoller.rb


This is my first contribution here, feedback on improving this or future postings is appreciated :)

Ruby on Rails Tutorial: Learn Web Development with Rails 4

While working through chapter 10.3, I got stuck. In the end, a misspelled file name had me chasing ghosts for a few days.

$ rspec spec/requests/authentication_pages_spec.rb
No DRb server is running. Running in local process instead ...
...FF................

Failures: 

1) Authentication authorization for non-signed-in users in the Microposts controller submitting to the create action 
Failure/Error: before { post microposts_path } 
ActionController::RoutingError: 
uninitialized constant MicropostsController 
# ./spec/requests/authentication_pages_spec.rb:93:in `block (6 levels) in ' 

2) Authentication authorization for non-signed-in users in the Microposts controller submitting to the destroy action 
Failure/Error: before { delete micropost_path(FactoryGirl.create(:micropost)) } 
ActionController::RoutingError: 
uninitialized constant MicropostsController 
# ./spec/requests/authentication_pages_spec.rb:98:in `block (6 levels) in ' 

Finished in 0.92253 seconds 
21 examples, 2 failures 

Failed examples: 

rspec ./spec/requests/authentication_pages_spec.rb:94 # Authentication authorization for non-signed-in users in the Microposts controller submitting to the create action 
rspec ./spec/requests/authentication_pages_spec.rb:99 # Authentication authorization for non-signed-in users in the Microposts controller submitting to the destroy action

Upvotes: 27

Views: 36298

Answers (6)

Ahmed Elkoussy
Ahmed Elkoussy

Reputation: 8568

Just to help if someone gets stuck with a similar issue:

I misspelled the controller, if you type without the s in products it was wrong:

Wrong:

get '/my_products', to: 'product#my_products'

Right:

get '/my_products', to: 'products#my_products'

Upvotes: 3

Elijah Murray
Elijah Murray

Reputation: 2172

I had incorrectly included the below in my application_controller.rb

Correct: include ActionController::MimeResponds

Incorrect: include ActionController::MimeResponse

# /controllers/api/v1/application_controller.rb

module Api
  module V1
    class ApplicationController < ActionController::API
      include ActionController::MimeResponds
    end
  end
end

Upvotes: 0

Wei Liu
Wei Liu

Reputation: 13

in my routes: i had "/"instead of "#" for all the "get", so change that to "#" get 'all' => 'storefront#all_items'

get 'categorical' => 'storefront#items_by_category'

get 'branding' => 'storefront#items_by_brand'

that fixed all my errors.

Upvotes: 0

thedanotto
thedanotto

Reputation: 7327

In routes.rb I typed resource instead of resources

Upvotes: 2

juliangonzalez
juliangonzalez

Reputation: 4381

This can also happen if you have a nested route mapping a nested directory:

Started POST "/brokers/properties/5/images/upload" for ...

ActionController::RoutingError (uninitialized constant Brokers::ImagesController):

namespace :brokers do
  resources :properties, only: [] do
    collection do
      post 'upload'
    end
    member do
      resources :images, only: [] do
        collection do
          post 'upload'
        end
      end
    end
  end
end

You Must place your images_controller.rb file with the following structure:

-controllers
 |-brokers
   |-images_controller.rb

Notice in the directory structure images_controller.rb is direct descendant of brokers.

So in order to let Rails find your class don't create a subdirectory properties inside brokers mapping the route structure, it has to be direct descendent of brokers

Upvotes: 6

8legged
8legged

Reputation: 662

This was due to a misspelled file name ~/sample_app/app/controllers/microposts_controller.rb (was microposts_contoller.rb)

Upvotes: 29

Related Questions